我正在尝试将一个类添加到每个具有更多(或等于或少于)2 个 li 的 ul。
不知怎么的,我无法让它继续下去。我要么最终将一个类添加到所有 2 级 ul,要么不添加。
这是我正在使用的代码:
$(document).ready(function() {
var countli = $('ul.level_2 li').length;
if(countli >= 3) {
$("ul.level_2").addClass("short");
}
});
我错过了什么?
最佳答案
你应该使用 .filter()
Reduce the set of matched elements to those that match the selector or pass the function's test.
脚本
$("ul.level_2").filter(function () {
return $(this).find('li').length >= 3
}).addClass("short");
关于javascript - 向具有更多(或)少于 2 个 li 的 ul 添加一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32265889/