如何使用 .hide()
、.show()
或 .toggle()
切换元素的可见性?
如何测试元素是可见
还是隐藏
?
最佳答案
由于问题涉及单个元素,因此这段代码可能更合适:
// Checks CSS content for display:[none|block], ignores visibility:[true|false]
$(element).is(":visible");
// The same works with hidden
$(element).is(":hidden");
与twernt's suggestion相同, 但应用于单个元素;它matches the algorithm recommended in the jQuery FAQ .
我们使用 jQuery 的 is()使用另一个元素、选择器或任何 jQuery 对象检查所选元素。此方法遍历 DOM 元素以找到满足传递参数的匹配项。如果匹配则返回真,否则返回假。
关于javascript - 如何检查元素是否隐藏在 jQuery 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45449241/