<body>
<div></div>
<script>
var div=document.getElementsByTagName('div')[0];
div.style.backgroundColor="green";
div.style.height="100px";
div.style.width="100px";
var count=true;
div.onclick=function(){
if(count=true){
this.style.backgroundColor="red";
this.style.width="200px";
this.style.height="200px";
this.style.borderRadius="50%";
count=false;
}
if(count=false)
{
this.style.backgroundColor="green";
this.style.height="100px";
this.style.width="100px";
count=true;
}
}
</script>
</body>
我点击了一下图片,用控制台查看时,count的值确实由true转为false了,但是为什么再点时图片切换不了,不懂哪里错了。
if中的=改为==,=是赋值了,点击后最后的if就是给count赋值为false,无法再改了
if(count=true){
==>>
if(count==true){
if(count=false)
===>
if(count==false)
而且你应该改为if else结构。。二个并列的if语句,上面为改为false后又执行一次false判断为真又改回true了
<body>
<div></div>
<script>
var div=document.getElementsByTagName('div')[0];
div.style.backgroundColor="green";
div.style.height="100px";
div.style.width="100px";
var count=true;
div.onclick=function(){
if(count==true){
this.style.backgroundColor="red";
this.style.width="200px";
this.style.height="200px";
this.style.borderRadius="50%";
count=false;
}
else
{
this.style.backgroundColor="green";
this.style.height="100px";
this.style.width="100px";
count=true;
}console.log(count)
}
</script>
</body>