gpt4 book ai didi

javascript - 如何在 JavaScript 中使用 getElementById ("").style.color 更改对象的颜色

转载 作者:行者123 更新时间:2023-12-04 00:02:58 24 4
gpt4 key购买 nike

当单击button2(名为“蓝色”)时,我试图将框的颜色更改为蓝色。

这是不起作用的代码......有人可以帮忙吗?

演示

  document.getElementById("button1").addEventListener("click", function(){
document.getElementById("box").style.height = "300px";
document.getElementById("box").style.width = "300px";
});


document.getElementById("button2").addEventListener("click", function(){

document.getElementById("box").style.color = 'blue';
});
<!DOCTYPE html>
<html>
<head>
<title>Jiggle Into JavaScript</title>
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> -->
</head>
<body>

<p>Press the buttons to change the box!</p>

<div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>

<button id="button1">Grow</button>
<button id="button2">Blue</button>
<button id="button3">Fade</button>
<button id="button4">Reset</button>

<script type="text/javascript" src="Javascript.js"></script>



</body>
</html>

最佳答案

更改 HTML 元素的背景颜色而不是颜色属性。

document.getElementById("button2").addEventListener("click", function(){    
document.getElementById("box").style.backgroundColor = "blue";
});

请在下面找到工作片段:

document.getElementById("button2").addEventListener("click", function(){    
document.getElementById("box").style.backgroundColor = "blue";
});
<!DOCTYPE html>
<html>
<head>
<title>Jiggle Into JavaScript</title>
</head>
<body>

<p>Press the buttons to change the box!</p>

<div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>

<button id="button1">Grow</button>
<button id="button2">Blue</button>
<button id="button3">Fade</button>
<button id="button4">Reset</button>

</body>
</html>

关于javascript - 如何在 JavaScript 中使用 getElementById ("").style.color 更改对象的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60702344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com