onclick事件里的else if没反应


<span style="color: black;">11111</span>
    <script>
        document.getElementsByTagName('span')[0].onclick=function(){
            if(this.style.color="black"){
                this.style.color="red"
            }else if(this.style.color="red"){
                this.style.color="black"
            }
        }
    </script>

第一次点击后有效果,第二次点击没反应

this.style.color="black" 是赋值,==才是判断,你直接赋值了black,接着执行赋值了red,else if肯定不执行了啊

这样改

 
<span style="color: black;">11111</span>
    <script>
        document.getElementsByTagName('span')[0].onclick=function(){
            if(this.style.color=="black"){
                this.style.color="red"
            }else if(this.style.color=="red"){
                this.style.color="black"
            }
        }
    </script>