<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 Python爬取指定微博话题下的内容,保存为txt
- ¥15 vue2登录调用后端接口如何实现
- ¥65 永磁型步进电机PID算法
- ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
- ¥88 找成都本地经验丰富懂小程序开发的技术大咖
- ¥15 如何处理复杂数据表格的除法运算
- ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
- ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
- ¥15 latex怎么处理论文引理引用参考文献
- ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?