qq_40805263 2018-10-13 08:44 采纳率: 41.7%
浏览 859
已采纳

js为什么点击图片,图片切换不了



<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了,但是为什么再点时图片切换不了,不懂哪里错了。
  • 写回答

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 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))