gpt4 book ai didi

javascript - 单击时如何更改文本颜色

转载 作者:行者123 更新时间:2023-12-02 18:52:35 25 4
gpt4 key购买 nike

我想在点击文本时更改文本的颜色。谁能告诉我为什么这段代码不起作用?

<html>
<body>
<center>
<div id="web" style="color:black" onclick="changeformat(this.id)"> Web </div>
<div id="img" style="color:blue" onclick="changeformat(this.id)"> Images </div>
<div id="news" style="color:blue" onclick="changeformat(this.id)"> News </div>
</center>
</body>
</html>

<script>
function changeformat(type)
{
document.getElementById('web').style = "color:blue;";
document.getElementById('img').style = "color:blue";
document.getElementById('news').style = "color:blue";
document.getElementById(type).style = "color:black";
}
</script>

最佳答案

试试这个,它会起作用的。更改颜色的正确方法是使用:document.getElementById(id).style.color

<html>
<body>
<center>
<div id="web" style="color:black" onclick="changeformat(this.id)"> Web </div>
<div id="img" style="color:blue" onclick="changeformat(this.id)"> Images </div>
<div id="news" style="color:blue" onclick="changeformat(this.id)"> News </div>
</center>
</body>
</html>

<script>
function changeformat(type)
{
document.getElementById('web').style.color = "blue";
document.getElementById('img').style.color = "blue";
document.getElementById('news').style.color = "blue";
document.getElementById(type).style.color = "black";
}
</script>

关于javascript - 单击时如何更改文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15674071/

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