尝试获取样式时的SyntaxError

cel*_*yer 0 html javascript dom styles web

我正在尝试创建一个函数,将对象的颜色淡化为Javascript中的另一种颜色作为链接,但是当我使用Firebug的JavaScript控制台时,控制台会给我一个SyntaxError:

SyntaxError: syntax error
[Break On This Error] if (document.getElementById(fadeID).style.color = "#E6E6E6")){ 
Run Code Online (Sandbox Code Playgroud)

Javascript(我正在使用警报来测试它是否正常工作.如果我将它放在if l之外:

<script>
    function toggleColor(fadeID){
        if (document.getElementById(fadeID).style.color = "#E6E6E6")){
            alert("hi");
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

HTML:

<body>
<div id = "content">
    <div id = "header">
        <a onmouseover = "javascript:toggleColor('home')" id = "home" href = "#" title = "Home">Home</a> |
        <a onmouseover = "javascript:toggleColor('music')" id = "music" href = "#" title = "Music">Music</a>
        </h1>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Gur*_*ngh 5

 if (document.getElementById(fadeID).style.color = "#E6E6E6")){
Run Code Online (Sandbox Code Playgroud)

应该

 if (document.getElementById(fadeID).style.color == "#E6E6E6"){
Run Code Online (Sandbox Code Playgroud)

您将颜色指定为=,而不是比较.见FIDDLE