【问题标题】:jQuery hide show div doesn't work in Internet ExplorerjQuery 隐藏显示 div 在 Internet Explorer 中不起作用
【发布时间】:2016-12-28 05:01:51
【问题描述】:

当我点击togglediv 时,commentdiv 必须可见或隐藏。以下代码在 Firefox 而不是 Internet Explorer 上运行:

$(document).ready(function(){
    $("#togglediv").click(function(){ 
        if( $("#commentdiv").is(":visible") ) {
            $("#commentdiv").hide("slow");
            $("#togglediv").text("show");
        } else {
            $("#commentdiv").show("slow");
            $("#togglediv").text("hide");
        }
    });
});

【问题讨论】:

    标签: jquery internet-explorer


    【解决方案1】:

    在 jquery 中有一个函数 toggle 可以完全按照您的要求进行操作,而无需检查可见性:

    $("#commentdiv").toggle("slow");
    

    【讨论】:

      【解决方案2】:

      我会尝试:

      $(document).ready(function() {
        $("#togglediv").click(function() {
          // note: do this first because the :hidden test fails if you
          // do it after triggering a slow animation
          $("#togglediv").text($("#commentdiv").is(":hidden") ? "Hide" : "Sgiw");
          $("#commentdiv").toggle('slow');
        });
      });
      

      编辑:响应您的评论,此示例在 IE7/FF3 中非常适合我。 注意:在使用慢速效果时,我确实必须颠倒语句的顺序。有趣!

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html>
      <head>
        <title>Test</title>
        <style type="text/css">
          #togglediv { padding: 5px; background-color: yellow; }
          #commentdiv { padding: 5px; background-color: #CCC; height: 100px; }
        </style>
      </head>
      <body>
        <div id="togglediv">Hide</div>
        <div id="commentdiv">thanks for answer. but i have tried this code, it was okay. i want to use toggle("slow") effect. this effect is runing firefox, but not i.e. is it a bug?</div>
        <script type="text/javascript" src="jquery-1.3.1.js"></script>
        <script type="text/javascript">
        $(function() {
          $("#togglediv").click(function() {
            $("#togglediv").text($("#commentdiv").is(":hidden") ? "Hide" : "Show");
            $("#commentdiv").toggle('slow');
          });
        });
        </script>
      </body>
      </html>
      

      【讨论】:

      • 感谢您的回答。但我试过这段代码,没关系。我想使用切换(“慢”)效果。这个效果是运行 firefox,但不是,即它是一个错误吗?
      【解决方案3】:

      你错过了一个结束 }

      试试

        $(document).ready(function(){
                   $("#togglediv").click(function(){
                      if($("#commentdiv").is(":visible")){
                      $("#commentdiv").hide("slow"); $("#togglediv").text("show");
                      }
                      else{
                      $("#commentdiv").show("slow"); $("#togglediv").text("hide");
                      }
                  }
              });
      

      【讨论】:

        【解决方案4】:

        我注意到一些有趣的事情,我认为上面的 cletus 已经提到了,如果你将“显示”行的顺序与“文本”行切换 - 它似乎开始工作了。我对此没有任何解释;很高兴知道幕后发生了什么。

        【讨论】:

          【解决方案5】:
          if(document.getElementById(ThisObj).style.display == 'none')
          {
              document.getElementById(ThisObj).style.display = 'block';
          }
          else
          {
              document.getElementById(ThisObj).style.display = 'none';
          }
          

          有效:)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-18
            • 1970-01-01
            • 2016-01-24
            相关资源
            最近更新 更多