如何检查元素是否隐藏在 jQuery 中? [英] How do I check if an element is hidden in jQuery?
问题描述
是否可以使用函数.hide()
、.show()
或.toggle()
来切换元素的可见性代码>?
Is it possible to toggle the visibility of an element, using the functions .hide()
, .show()
or .toggle()
?
你将如何测试一个元素是visible
还是hidden
?
How would you test if an element is visible
or hidden
?
推荐答案
由于问题涉及单个元素,所以这段代码可能更合适:
Since the question refers to a single element, this code might be more suitable:
// Checks CSS content for display:[none|block], ignores visibility:[true|false]
$(element).is(":visible");
// The same works with hidden
$(element).is(":hidden");
同twernt 的建议,但适用于单个元素;它匹配推荐的算法在 jQuery 常见问题解答中.
It is the same as twernt's suggestion, but applied to a single element; and it matches the algorithm recommended in the jQuery FAQ.
我们使用 jQuery 的 is() 用另一个元素、选择器或任何 jQuery 检查所选元素目的.该方法沿着DOM元素遍历,找到一个满足传入参数的匹配项.匹配则返回真,否则返回假.
We use jQuery's is() to check the selected element with another element, selector or any jQuery object. This method traverses along the DOM elements to find a match, which satisfies the passed parameter. It will return true if there is a match, otherwise return false.
这篇关于如何检查元素是否隐藏在 jQuery 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!