<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了,但是为什么再点时图片切换不了,不懂哪里错了。
js为什么点击图片,图片切换不了
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- Go 旅城通票 2018-10-13 09:02关注
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>
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记
- ¥15 保护模式-系统加载-段寄存器
- ¥15 电脑桌面设定一个区域禁止鼠标操作
- ¥15 求NPF226060磁芯的详细资料