admin管理员组文章数量:1410674
I'm trying to add a class to every ul that has more (or equal&less than) 2 li's.
Somehow I can't get it going. I either end up with a class added to all level 2 ul's or to none.
Here's the code I'm, using:
$(document).ready(function() {
var countli = $('ul.level_2 li').length;
if(countli >= 3) {
$("ul.level_2").addClass("short");
}
});
What am I missing?
/
I'm trying to add a class to every ul that has more (or equal&less than) 2 li's.
Somehow I can't get it going. I either end up with a class added to all level 2 ul's or to none.
Here's the code I'm, using:
$(document).ready(function() {
var countli = $('ul.level_2 li').length;
if(countli >= 3) {
$("ul.level_2").addClass("short");
}
});
What am I missing?
http://jsfiddle/gmodesignz/20dksk4y/3/
Share asked Aug 28, 2015 at 7:46 The GmoThe Gmo 2743 silver badges12 bronze badges 1-
You count
li
s in allul
s at once and add class to allul
s if total count is>= 3
. – Regent Commented Aug 28, 2015 at 7:51
4 Answers
Reset to default 7You should use .filter()
Reduce the set of matched elements to those that match the selector or pass the function's test.
Script
$("ul.level_2").filter(function () {
return $(this).find('li').length >= 3
}).addClass("short");
DEMO
What you are doing is you are generalizing everything. Make it specific inside the closures. See the below snippet:
$(document).ready(function() {
$('ul.level_2').each(function () {
if ($(this).children().length > 2)
$(this).addClass("short");
});
});
* {font-family: 'Segoe UI';}
ul {color: blue;}
.short {color: red;}
.large {color: green;}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul>
<li>No Submenu</li>
<li class="sub">
Long Submenu
<ul class="level_2">
<li>Something</li>
<li>Something</li>
<li>Something</li>
<li>Something</li>
<li>Something</li>
</ul>
</li>
<li class="sub">
Short Submenu
<ul class="level_2">
<li>Something</li>
<li>Something</li>
</ul>
</li>
</ul>
Also you must apply the class to the parents and not the children.
You can add the class to ul
elements which has a 3rd li
child(ie has more than 2 li
) like
$(document).ready(function() {
$('ul.level_2').has('li:eq(2)').addClass("short");
});
ul {
color: blue;
}
.short {
color: red;
}
.large {
color: green;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
<li>No Submenu</li>
<li class="sub">
Long Submenu
<ul class="level_2">
<li>Something</li>
<li>Something</li>
<li>Something</li>
<li>Something</li>
<li>Something</li>
</ul>
</li>
<li class="sub">
Short Submenu
<ul class="level_2">
<li>Something</li>
<li>Something</li>
</ul>
</li>
</ul>
$(document).ready(function() {
$("ul .level_2").each(function(){
var countLi = $(this).find("li").length;
if(countLi <= 3 ){
$(this).find("li").addClass("short");
}
});
});
http://jsfiddle/OsamaMohamed/wsu8xwwg/
本文标签: javascriptAdding a class to an ul that has more (or) less than 2 li39sStack Overflow
版权声明:本文标题:javascript - Adding a class to an ul that has more (or) less than 2 li's - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745068713a2640680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论