如何测试某个元素是否已经可见? [英] How can I test if an element is already visible?
问题描述
在HTML代码中,我使用jQuery隐藏或显示具有ID的元素.如何测试某个元素是否已经可见?
In the HTML code, I use jQuery to hide or display an element having an ID. How can I test if an element is already visible?
例如,我们使用show()
来显示具有ID的元素.但是在调用show()
之前,我需要测试show()
是否已用于该ID,然后如何比较?
For example, we use show()
to display an element having an ID. But before calling show()
, I need to test whether show()
has been already used for that ID then how to compare?
推荐答案
如果该元素可见,则show()不会执行任何操作,因此风险很小.但是,可以使用:visible
或:hidden
选择器来查找可见元素.
If the element is visible, show() won't do anything, so it's pretty low risk. However, you can use the :visible
or :hidden
selector to find visible elements.
$('#myId:visible').hide();
$('#myId:hidden').show();
在选择器中使用此选项,您可以触发显示和隐藏方法,而不必担心可见的内容,因为如果您尝试隐藏:hidden
元素,则不会选择任何内容,因此您可以什么都不要做.
Using this in your selector, you can just fire off the show and hide methods and not worry about what is visible or not, because if you try to hide a :hidden
element, you won't select anything so you won't do anything.
这篇关于如何测试某个元素是否已经可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!