gpt4 book ai didi

JavaScript 通过点击设置新的背景颜色

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:55 25 4
gpt4 key购买 nike

无法更改 onclick 下面部分的背景颜色,试图找出不正确的地方。

<!DOCTYPE html>
<html>
<head>
<script>
window.onload=function(){
document.getElementById("top_bar").getPropertyValue("background-color").onclick=function(){
this="green";
}
}
</script>
</head>
<body>
<section id="top_bar"></section>
</body>
</html>

最佳答案

document.getElementById("top_bar").addEventListener('click', function() {
this.style.backgroundColor = "green";
}
// I like the above code better, because you don't see the `onclick` in the DOM, but this would be more like your approach:
document.getElementById("top_bar").onclick = function() {
this.style.backgroundColor = "green";
}

阅读有关 getPropertyValue 函数的更多信息 here

关于JavaScript 通过点击设置新的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22908878/

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