jQuery计数ul中的li元素->长度? [英] jquery count li elements inside ul -> length?
本文介绍了jQuery计数ul中的li元素->长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果ul
内部包含多个li
元素,则应该发生某些情况,否则不会发生!
If a ul
has more than one li
-element inside of it, something should happen, otherwise not!
我在做什么错了?
if ( $('#menu ul').length > 1 ) {
推荐答案
您必须计算li元素而不是ul元素:
You have to count the li elements not the ul elements:
if ( $('#menu ul li').length > 1 ) {
如果需要每个包含至少两个LI元素的UL元素,请使用过滤器功能:
If you need every UL element containing at least two LI elements, use the filter function:
$('#menu ul').filter(function(){ return $(this).children("li").length > 1 })
您也可以根据自己的情况使用它:
You can also use that in your condition:
if ( $('#menu ul').filter(function(){ return $(this).children("li").length > 1 }).length) {
这篇关于jQuery计数ul中的li元素->长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文