html按钮点击改变颜色代码,HTML/CSS/JS在单击时更改多个按钮的颜色

本文介绍了一个HTML/CSS项目,旨在通过点击不同按钮来改变五级条的颜色。作者尝试在点击特定按钮时使该按钮及其之前的按钮变为红色,但遇到了Chrome扩展策略限制内联JS的问题。文章探讨了解决方案,包括将JS代码移至单独文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nullvar level1 = document.getElementById("bar1");

var level2 = document.getElementById("bar2");

var level3 = document.getElementById("bar3");

var level4 = document.getElementById("bar4");

var level5 = document.getElementById("bar5");

var red = '#000000';

var white = '#FFFFFF';

document.addEventListner('DOMContentLoaded',function() {

level1.addEventListner('click', function() {

changeColor1();

});

});

document.addEventListner('DOMContentLoaded',function() {

level2.addEventListner('click', function() {

changeColor2();

});

});

document.addEventListner('DOMContentLoaded',function() {

level3.addEventListner('click', function() {

changeColor3();

});

});

document.addEventListner('DOMContentLoaded',function() {

level4.addEventListner('click', function() {

changeColor4();

});

});

document.addEventListner('DOMContentLoaded',function() {

level5.addEventListner('click', function() {

changeColor5();

});

});

function changeColor1(){

level1.style.backgroundColor = red;

level2.style.backgroundColor = white;

level3.style.backgroundColor = white;

level4.style.backgroundColor = white;

level5.style.backgroundColor = white;

}

function changeColor2(){

level1.style.backgroundColor = red;

level2.style.backgroundColor = red;

level3.style.backgroundColor = white;

level4.style.backgroundColor = white;

level5.style.backgroundColor = white;

}

function changeColor3(){

level1.style.backgroundColor = red;

level2.style.backgroundColor = red;

level3.style.backgroundColor = red;

level4.style.backgroundColor = white;

level5.style.backgroundColor = white;

}

function changeColor4(){

level1.style.backgroundColor = red;

level2.style.backgroundColor = red;

level3.style.backgroundColor = red;

level4.style.backgroundColor = red;

level5.style.backgroundColor = white;

}

function changeColor5(){

level1.style.backgroundColor = red;

level2.style.backgroundColor = red;

level3.style.backgroundColor = red;

level4.style.backgroundColor = red;

level5.style.backgroundColor = red;

}h1 {

color: black;

}

p {

color: black;

}

body {

width: 300px;

height: 300px;

}

Cat Dominates World

div {

background-color: none;

border: 0.5px solid black;

text-align: center;

display: inline-block;

cursor: pointer;

padding: 4px 24px;

}

Cat Dominates World

null

你好! 我现在正在使用HTML/CSS制作5级条,并尝试改变多个按钮的颜色。 例如,单击第三个按钮,第一个/第二个/第三个按钮的颜色将更改为红色。 我从对方的回答中修复了,然而,由于Chrome扩展策略,内联实现是不允许的。 我制作了另一个js文件来运行代码,但它不起作用。

代码有问题吗? 请给我反馈:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值