jquery:如果孩子有特定class级?
我想知道如何确定ul是否有超过2个孩子,如果有两个孩子在这个ul中有两个特定的类…
if($(this).children().length > 2 && $(this).children('.section-title, .active')) { $(this).append('… '); }
???
var $ul = $('ul'); if($ul.find("li").length > 2 && $ul.find('.active, .inactive').length == 2) { alert('yes, it is this way'); } - Whatever
- Whatever
- Whatever
- Whatever
演示: http : //jsfiddle.net/RtTSM/1/
var ulChildren = $("li", this); if (ulChildren.length > 2 && $('li.section-title', ulChildren).length >= 1 && $('li.active', ulChildren).length >= 1)
这将检查以下规则:
- ul下有两个以上的li元素
- 至少有一个li属于section-title类
- 至少有一个具有活跃类别的li
var ul = $('#ul_id'); if ($('.class1, .class2', ul).size()>2)
你不需要测试第一个条件(有两个以上的孩子),因为它是一个“和”条件,如果你的第二个条件满足,第一个条件是微不足道的。
詹姆斯林