// Checks CSS content for display:[none|block], ignores visibility:[true|false]$(element).is(":visible");
// The same works with hidden$(element).is(":hidden");
因为Elements with visibility: hidden or opacity: 0 are considered visible, since they still consume space in the layout(如jQuery:可见选择器所述)-我们可以通过这种方式检查元素真的是否可见:
Android 4.4(本地浏览器/Chrome /Firefox)、Firefox(Windows/Mac)、Chrome(Windows/Mac)、Opera(WindowsPresto/Mac WebKit)、Internet Explorer(Internet Explorer 5-11文档模式+虚拟机上的Internet Explorer 8)和Safari(Windows/Mac/iOS)。
// using a pure CSS selectorif ($('p:visible')) {alert('Paragraphs are visible (checked using a CSS selector) !');};
// using jQuery's is() methodif ($('p').is(':visible')) {alert('Paragraphs are visible (checked using is() method)!');};
// using jQuery's filter() methodif ($('p').filter(':visible')) {alert('Paragraphs are visible (checked using filter() method)!');};
// you can use :hidden instead of :visible to reverse the logic and check if an element is hidden// if ($('p:hidden')) {// do something// };
$(document).ready(function(){$("#toggle").click(function(){$("#content").toggle();});
$("#visiblity").click(function(){if( $('#content').is(':visible') ){alert("visible"); // Put your code for visibility}else{alert("hidden");}});});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<p id="content">This is a Content</p>
<button id="toggle">Toggle Content Visibility</button><button id="visibility">Check Visibility</button>
<html><body><script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script><script>var vm = {IsDivVisible: ko.observable(true);}vm.toggle = function(data, event) {// Get current visibility state for the divvar x = IsDivVisible();// Set it to the oppositeIsDivVisible(!x);}ko.applyBinding(vm);</script><div data-bind="visible: IsDivVisible">Peekaboo!</div><button data-bind="click: toggle">Toggle the div's visibility</button></body></html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="showHide">Hide</button><div id="content"><h2>Some content</h2><p>What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p></div>
<script>if ($("#myelement").is(":visible")){alert ("#myelement is visible");}if ($("#myelement").is(":hidden")){alert ("#myelement is hidden"); }</script>
// You can also do this...
$("button").click(function(){// show hide paragraph on button click$("p").toggle("slow", function(){// check paragraph once toggle effect is completedif($("p").is(":visible")){alert("The paragraph is visible.");} else{alert("The paragraph is hidden.");}});});
function checkVisibility() {// check if element is hidden or not and return true falseconsole.log($('#element').is(':hidden'));
// check if element is visible or not and return true falseconsole.log($('#element').is(':visible'));
if ( $('#element').css('display') == 'none' || $('#element').css("visibility") == "hidden"){console.log('element is hidden');} else {console.log('element is visibile');}}
checkVisibility()$('#toggle').click(function() {$('#element').toggle()checkVisibility()})