取消隐藏div标签使用jQuery

问题描述:

http://www.frostjedi.com/terra/scripts/demo/jquery01.html取消隐藏div标签使用jQuery

点击页面上的按钮应该取消隐藏“你好,世界!” div标签不应该吗?当我点击它时,我得到一个$("#demo").style is undefined错误。

+0

当div已经可见时再次单击该按钮时会发生什么? – ThdK 2012-03-26 07:58:25

+0

有**九个答案,但直到没有人选择。 – Jigs 2012-03-26 11:55:56

您应该使用

$('#demo').show(); // to display 

$('#demo').hide(); // to hide the div 

尝试.show()

$("#demo").show() 

使用hide() method

$("#demo").hide() 

$("#demo").hide() 

甚至toggle() method切换能见度:

$("#demo").toggle() 
在您的示例

综合:从jQuery的documentation

​​

实施例:

<h1 class="firstHeading" style="display: block;">Tutorials:Basic Show and Hide</h1> 
<div class="examples"> 
    <input id="hideh1" type="submit" value="Hide site tile" name="hideh1"> 
    <input id="showh1" type="submit" value="Show site title" name="showh1"> 
    <input id="toggleh1" type="submit" value="Toggle site title" name="toggleh1"> 
</div> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $(document).ready(function() { 
      $('#hideh1').click(function() { 
       $('div.showhide,h1').hide(); 
      }); 
      $('#showh1').click(function() { 
       $('div.showhide,h1').show(); 
      }); 
      $('#toggleh1').click(function() { 
       $('div.showhide,h1').toggle(); 
      }); 
     }); 
    }); 
</script> 

试试这个,最简单的一个..

<input type="button" onclick="$('#demo').toggle()" value="clicking me should unhide a div tag"> 

$(document).ready(function() { 
    $("input[type='button']").click(function() { 
     $("#demo").show(); 
    }); 
}); 

document.getElementById("demo").style.display = "block"; 

$("#demo").style不是一个jQuery函数

您应该使用$("#demo").css("display","block")属性或使用show()toggle()函数

$("#button").click(function() { 
     $("#div").hide(); 
}); 
$("#button").click(function() { 
    $("#div").show(2000); 
}); 

您无法通过访问jQuery对象的属性。 (点)(它们不是JavaScript对象)

你,

$('#demo').css({'display':'none'}) 

或者你可以使用$('#demo').hide();(它的快捷方式)

jQuery.hide() is very slow

改用以下:

document.getElementById('demo').style.display = 'none' 

这是你听到style财产。该属性适用于DOM对象。 jQuery对象不是DOM对象。