如何检查 jQuery 中的元素是否隐藏?
如何使用 .hide()
、.show()
或 .toggle()
切换元素的可见性?
如何测试元素是可见
还是隐藏
?
How do I toggle the visibility of an element using .hide()
, .show()
, or .toggle()
?
How do I test if an element is visible
or hidden
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
另外,这里还有一个三元条件表达式,用于检查元素的状态,然后切换它:
Also here's a ternary conditional expression to check the state of the element and then to toggle it:
由于问题涉及单个元素,因此此代码可能更合适:
它与 twernt 的建议,但应用于单个元素; 并且它与推荐的算法匹配在 jQuery 常见问题解答中。
我们使用 jQuery 的 is() 来检查所选元素与另一个元素、选择器或任何 jQuery 对象。 该方法沿着 DOM 元素遍历以查找满足传递参数的匹配项。 如果匹配则返回 true,否则返回 false。
Since the question refers to a single element, this code might be more suitable:
It is the same as twernt's suggestion, but applied to a single element; and it matches the algorithm recommended in the jQuery FAQ.
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 中针对
:hidden
选择器测试元素时,应考虑到绝对定位元素可能会被识别为隐藏,尽管其子元素可见。首先,这似乎有点违反直觉——尽管仔细查看 jQuery 文档会给出相关信息:
因此,这对于框模型和元素的计算样式实际上是有意义的。 即使宽度和高度没有显式设置为0,它们也可以隐式设置。
看一下下面的例子:
jQuery 3.x 更新:
使用 jQuery 3,所描述的行为将会改变! 如果元素具有任何布局框(包括零宽度和/或高度的布局框),则元素将被视为可见。
JSFiddle 与 jQuery 3.0.0-alpha1:
http://jsfiddle.net/pM2q3/7/
相同的 JavaScript 代码将具有以下输出:
When testing an element against
:hidden
selector in jQuery it should be considered that an absolute positioned element may be recognized as hidden although their child elements are visible.This seems somewhat counter-intuitive in the first place – though having a closer look at the jQuery documentation gives the relevant information:
So this actually makes sense in regards to the box-model and the computed style for the element. Even if width and height are not set explicitly to 0 they may be set implicitly.
Have a look at the following example:
Update for jQuery 3.x:
With jQuery 3 the described behavior will change! Elements will be considered visible if they have any layout boxes, including those of zero width and/or height.
JSFiddle with jQuery 3.0.0-alpha1:
http://jsfiddle.net/pM2q3/7/
The same JavaScript code will then have this output:
您应该考虑的另一个答案是,如果您要隐藏元素,则应该使用 jQuery,但是您没有实际隐藏它,而是删除整个元素,但复制其 HTML 内容和标签将其自身放入 jQuery 变量中,然后您需要做的就是使用普通的
if (!$('#thetagname').length)
测试屏幕上是否存在这样的标签。Another answer you should put into consideration is if you are hiding an element, you should use jQuery, but instead of actually hiding it, you remove the whole element, but you copy its HTML content and the tag itself into a jQuery variable, and then all you need to do is test if there is such a tag on the screen, using the normal
if (!$('#thetagname').length)
.您可以简单地使用
hidden
或visible
属性,例如:或者您可以使用 is 进行简化,如下所示。
One can simply use the
hidden
orvisible
attribute, like:Or you can simplify the same with is as follows.
您可以使用
隐藏
选择器:以及
可见
选择器:You can use the
hidden
selector:And the
visible
selector:上述方法没有考虑父级的可见性。 要同时考虑父级,您应该使用
.is(":hidden")
或.is(":visible")
。例如,
The above method does not consider the visibility of the parent. To consider the parent as well, you should use
.is(":hidden")
or.is(":visible")
.For example,
根据 jQuery 文档的
:visible
选择器:这在某些情况下很有用,但在其他情况下没用,因为如果您想检查元素是否可见(
display != none
),忽略父元素的可见性,您会发现执行. css("display") == 'none'
不仅更快,而且还能正确返回可见性检查。如果您想检查可见性而不是显示,则应使用:
.css("visibility") == "hidden"
。另请考虑附加 jQuery 注释:
另外,如果您担心性能,您应该检查现在你看到我了...显示/隐藏性能 (2010-05-04)。 并使用其他方法来显示和隐藏元素。
The
:visible
selector according to the jQuery documentation:This is useful in some cases and useless in others, because if you want to check if the element is visible (
display != none
), ignoring the parents visibility, you will find that doing.css("display") == 'none'
is not only faster, but will also return the visibility check correctly.If you want to check visibility instead of display, you should use:
.css("visibility") == "hidden"
.Also take into consideration the additional jQuery notes:
Also, if you are concerned about performance, you should check Now you see me… show/hide performance (2010-05-04). And use other methods to show and hide elements.
通常,当检查某些东西是否可见时,您会立即继续并用它做其他事情。 jQuery 链接使这一切变得简单。
因此,如果您有一个选择器,并且只想在可见或隐藏时对其执行某些操作,则可以使用
filter(":visible")
或filter(":hidden")
然后将其与您要执行的操作链接起来。因此,不要使用
if
语句,如下所示:或者更有效:
您可以在一行中完成所有操作:
Often when checking if something is visible or not, you are going to go right ahead immediately and do something else with it. jQuery chaining makes this easy.
So if you have a selector and you want to perform some action on it only if is visible or hidden, you can use
filter(":visible")
orfilter(":hidden")
followed by chaining it with the action you want to take.So instead of an
if
statement, like this:Or more efficiently:
You can do it all in one line:
来自 如何确定切换元素的状态?
您可以使用
:visible
和确定元素是否折叠:隐藏的
选择器。如果您只是根据元素的可见性对其进行操作,则只需在选择器表达式中包含
:visible
或:hidden
即可。 例如:From How do I determine the state of a toggled element?
You can determine whether an element is collapsed or not by using the
:visible
and:hidden
selectors.If you're simply acting on an element based on its visibility, you can just include
:visible
or:hidden
in the selector expression. For example:这些答案都没有解决我所理解的问题,这就是我正在寻找的问题,“如何处理具有
可见性:隐藏
的项目?”。:visible
和:hidden
都不会处理这个问题,因为它们都在根据文档寻找显示。 据我所知,没有选择器来处理 CSS 可见性。 这是我解决它的方法(标准 jQuery 选择器,可能有更简洁的语法):None of these answers address what I understand to be the question, which is what I was searching for, "How do I handle items that have
visibility: hidden
?". Neither:visible
nor:hidden
will handle this, as they are both looking for display per the documentation. As far as I could determine, there is no selector to handle CSS visibility. Here is how I resolved it (standard jQuery selectors, there may be a more condensed syntax):我会使用 CSS class
.hide { display: none!important; }
。对于隐藏/显示,我调用
.addClass("hide")/.removeClass("hide")
。 为了检查可见性,我使用.hasClass("hide")
。如果您不打算使用
.toggle()
或.animate()
方法,这是一种检查/隐藏/显示元素的简单明了的方法。I would use CSS class
.hide { display: none!important; }
.For hiding/showing, I call
.addClass("hide")/.removeClass("hide")
. For checking visibility, I use.hasClass("hide")
.It's a simple and clear way to check/hide/show elements, if you don't plan to use
.toggle()
or.animate()
methods.演示链接
来源(来自我的博客):
Blogger 即插即用 - jQuery 工具和小部件:如何使用 jQuery 查看元素是否隐藏或可见
Demo Link
Source (from my blog):
Blogger Plug n Play - jQuery Tools and Widgets: How to See if Element is hidden or Visible Using jQuery
这对我有用,我使用
show()
和hide()
使我的 div 隐藏/可见:This works for me, and I am using
show()
andhide()
to make my div hidden/visible:您还可以使用纯 JavaScript 来执行此操作:
注意:
适用于任何地方
适用于嵌套元素
适用于 CSS 和内联样式
不需要框架
You can also do this using plain JavaScript:
Notes:
Works everywhere
Works for nested elements
Works for CSS and inline styles
Doesn't require a framework
元素可见性和 jQuery 的工作原理;
可以使用
display:none
、visibility:hidden
或opacity:0
隐藏元素。 这些方法的区别:display:none
隐藏元素,并且不占用任何空间;visibility:hidden
隐藏元素,但它仍然占用布局中的空间;opacity:0
将元素隐藏为“visibility:hidden”,但它仍然占用布局中的空间; 唯一的区别是不透明度可以使元素部分透明;有用的 jQuery 切换方法:
How element visibility and jQuery works;
An element could be hidden with
display:none
,visibility:hidden
oropacity:0
. The difference between those methods:display:none
hides the element, and it does not take up any space;visibility:hidden
hides the element, but it still takes up space in the layout;opacity:0
hides the element as "visibility:hidden", and it still takes up space in the layout; the only difference is that opacity lets one to make an element partly transparent;Useful jQuery toggle methods:
ebdiv
应设置为style="display:none;"
。 它适用于显示和隐藏:ebdiv
should be set tostyle="display:none;"
. It works for both show and hide:使用可见检查广告拦截器是否已激活的示例:
“ablockercheck”是adblocker 阻止的ID。 因此,检查它是否可见,您就可以检测广告拦截器是否已打开。
Example of using the visible check for adblocker is activated:
"ablockercheck" is a ID which adblocker blocks. So checking it if it is visible you are able to detect if adblocker is turned On.
毕竟没有一个例子适合我,所以我自己写了。
测试(不支持 Internet Explorer
filter:alpha
):a) 检查文档是否未隐藏
b) 检查元素的宽度/高度/不透明度是否为零内联样式中的
display:none
/visibility:hidden
c) 检查元素的中心(也因为它比测试每个像素/角更快)是否没有被其他元素隐藏元素(以及所有祖先,例如:
overflow:hidden
/滚动/一个元素覆盖另一个元素)或屏幕边缘d) 检查元素是否具有零宽度/高度/不透明度或
display:none< /code> / 可见性:隐藏在计算样式中(在所有祖先中)
在
Android 4.4(本机浏览器/Chrome/Firefox)、Firefox (Windows/Mac)、Chrome (Windows/Mac)、Opera 上测试(Windows Presto/Mac WebKit)、Internet Explorer (Internet Explorer 5- 11 种文档模式 + 虚拟机上的 Internet Explorer 8)和 Safari (Windows/Mac/iOS)。
如何使用:
After all, none of examples suits me, so I wrote my own.
Tests (no support of Internet Explorer
filter:alpha
):a) Check if the document is not hidden
b) Check if an element has zero width / height / opacity or
display:none
/visibility:hidden
in inline stylesc) Check if the center (also because it is faster than testing every pixel / corner) of element is not hidden by other element (and all ancestors, example:
overflow:hidden
/ scroll / one element over another) or screen edgesd) Check if an element has zero width / height / opacity or
display:none
/ visibility:hidden in computed styles (among all ancestors)Tested on
Android 4.4 (Native browser/Chrome/Firefox), Firefox (Windows/Mac), Chrome (Windows/Mac), Opera (Windows Presto/Mac WebKit), Internet Explorer (Internet Explorer 5-11 document modes + Internet Explorer 8 on a virtual machine), and Safari (Windows/Mac/iOS).
How to use:
要检查它是否不可见,我使用
!
:或者以下内容也是如此,将 jQuery 选择器保存在变量中,以便在多次需要时获得更好的性能:
To check if it is not visible I use
!
:Or the following is also the sam, saving the jQuery selector in a variable to have better performance when you need it multiple times:
使用类切换,而不是样式编辑。 。 。
使用指定用于“隐藏”元素的类很容易,也是最有效的方法之一。 使用
Display
样式“none”切换“隐藏”类将比直接编辑该样式执行得更快。 我在 Stack Overflow 问题中非常彻底地解释了其中的一些内容将同一 div 中的两个元素设置为可见/隐藏。JavaScript 最佳实践和优化
这是 Google 前端工程师 Nicholas Zakas 的一个真正具有启发性的 Google 技术讲座视频:
Use class toggling, not style editing . . .
Using classes designated for "hiding" elements is easy and also one of the most efficient methods. Toggling a class 'hidden' with a
Display
style of 'none' will perform faster than editing that style directly. I explained some of this pretty thoroughly in Stack Overflow question Turning two elements visible/hidden in the same div.JavaScript Best Practices and Optimization
Here is a truly enlightening video of a Google Tech Talk by Google front-end engineer Nicholas Zakas:
可以创建一个函数来检查可见性/显示属性,从而判断元素是否显示在 UI 中。
工作小提琴
A function can be created in order to check for visibility/display attributes in order to gauge whether the element is shown in the UI or not.
Working Fiddle
但是如果元素的 CSS 如下所示怎么办?
因此,还应该考虑这个 Stack Overflow 问题的答案如何检查元素是否在屏幕外 。
But what if the element's CSS is like the following?
So this answer to Stack Overflow question How to check if an element is off-screen should also be considered.
您需要检查两者。 显示和可见性:
如果我们检查
$this.is(":visible")
,jQuery 会自动检查这两件事。You need to check both. Display as well as visibility:
If we check for
$this.is(":visible")
, jQuery checks for both the things automatically.只需通过检查布尔值来检查可见性,例如:
我为每个函数使用了此代码。 否则,您可以使用
is(':visible')
来检查元素的可见性。Simply check visibility by checking for a boolean value, like:
I used this code for each function. Otherwise you can use
is(':visible')
for checking the visibility of an element.因为
具有visibility:hidden或opacity:0的元素被认为是可见的,因为它们仍然消耗布局中的空间
(如jQuery :visible Selector) - 我们可以通过这种方式检查元素是否真的可见:Because
Elements with visibility: hidden or opacity: 0 are considered visible, since they still consume space in the layout
(as described for jQuery :visible Selector) - we can check if element is really visible in this way: