JQuery - 如何计算选择器选择的元素数量? [英] JQuery - How do I count the number of elements selected by a selector?

查看:115
本文介绍了JQuery - 如何计算选择器选择的元素数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用$()。fadeOut()来淡化列表中的项目(< li>< / li>)。当列表为空时我想隐藏一个父对象。

I am using $().fadeOut() to fade items out in a list ( < li> < /li>). When the list is empty I wish to hide a parent object.

我打算通过检查我的触发事件来做到这一点,如果对象的数量是0然后隐藏父元素。如果需要,我可以使用fadeOut回调删除元素。

I plan on doing this by checking in my trigger event that fades the list if the count of the objects is 0 then hide the parent element. I can use the fadeOut callback to remove the elements if necessary.

点问题:
如何在ul中选择li标签然后使用jquery获取它们的总数?

推荐答案

像这样:

$('ul > li').length

更优雅的方法是写

$('ul:empty').fadeOut();

如果 ul 不为空,选择器不会匹配任何内容,代码将不执行任何操作。

If the ul is not empty, the selector won't match anything, and the code will do nothing.

您可能需要编写

$('.SomeContainer:has(ul:empty)').fadeOut();

这篇关于JQuery - 如何计算选择器选择的元素数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆