0

我想在单击它时更改文本的颜色。谁能让我知道为什么这段代码不起作用?

<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>
4

5 回答 5

1

你几乎拥有它,使用element.style.color

jsFiddle

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";
}

正如 Derek 指出的那样,您也可以使用element.setAttribute(),这将覆盖已经在元素上设置的其他内联样式。

jsFiddle

function changeformat(type) {
    document.getElementById('web').setAttribute("style", "color:blue;");
    document.getElementById('img').setAttribute("style", "color:blue");
    document.getElementById('news').setAttribute("style", "color:blue");
    document.getElementById(type).setAttribute("style", "color:black");
}
于 2013-03-28T04:05:41.623 回答
1

试试这个,它会工作。更改颜色的正确方法是使用: 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> 
于 2013-03-28T04:10:24.423 回答
0
<div id="web" style="color:black" onclick="changeformat(this)"> Web </div>
<div id="img" style="color:blue" onclick="changeformat(this)"> Images </div>
<div id="news" style="color:blue" onclick="changeformat(this)"> News </div>

function changeformat(element) {
    var elements = ['web', 'img', 'news'];
    for( var i = 0, length = elements.length; i < length; i++  ) {
        document.getElementById( elements[i] ).style.color = 'blue';
    }
    element.style.color = "black";
}

演示

于 2013-03-28T04:11:22.833 回答
0

您必须以这种方式设置颜色:

//element.style.CSSProperty = Value;
  ele.style.color = "blue";

优化版:

<div id="web" style="color:black" onclick="changeformat(event)"> Web </div>

function changeformat(e){
    var eles = document.querySelectorAll("div");
    for(var i = 0 ; i < eles.length; i++){
        eles[i].style.color = "blue";
    }
    e.target.style.color = "black";
}

演示:http: //jsfiddle.net/DerekL/V378R/2/

于 2013-03-28T04:11:58.693 回答
0

试试这个代码

function changeformat(type)
    {
    document.getElementById('web').style.color = 'green'; 
    document.getElementById('img').style.color = 'pink'; 
    document.getElementById('news').style.color = 'red'; 
    document.getElementById(type).style.color = "black";
    }
于 2013-03-28T04:24:45.817 回答