2009-02-04 122 views
4

当我点击togglediv,commentdiv必须是可见或隐藏的。下面的代码是在Firefox中运行,但不是Internet Explorer:jQuery hide show div在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"); 
     } 
    }); 
}); 

回答

5

我会尝试:

$(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> 
+0

感谢答案。但我已经试过这段代码,没关系。我想使用切换(“慢”)效果。这个效果是运行firefox,但不是一个错误? – ferixxx 2009-02-04 23:19:18

6

有一个功能切换 jQuery的,做你想要什么,而不必检查可见:

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

你”再缺少一个右}

尝试

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

东西,我注意到这很有趣,我认为这是上述由克莱图斯谈到,就是如果你将与“文本”行“秀”行的顺序 - 它似乎开始工作。我对此没有任何解释;很高兴知道幕后发生了什么。

1
if(document.getElementById(ThisObj).style.display == 'none') 
{ 
    document.getElementById(ThisObj).style.display = 'block'; 
} 
else 
{ 
    document.getElementById(ThisObj).style.display = 'none'; 
} 

工程:)