开发者

How to apply class as per the Count of the li tag?

开发者 https://www.devze.com 2022-12-19 00:35 出处:网络
Here is the structure of the HTML <div class=\"submenu\"> <ul> <li></li> <li></li>开发者_如何学编程

Here is the structure of the HTML

<div class="submenu">
<ul>
<li></li>
<li></li>开发者_如何学编程
</ul>
<ul>
    <li></li>
    <li></li>
    </ul>
<ul>
    <li></li>
    <li></li>
    </ul>
</div>

If there are three UL the the I want to add three-col class. two UL then two-col


Using Jquery, $(".submenu > ul").size() gives you the count.

This will set the class on the div with class submenu:

var count = $(".submenu > ul").size();
if(count == 3)
{
  $(".submenu").addClass("three-col");
}
else if(count == 2)
{
  $(".submenu").addClass("two-col");
}


$(function(){
  $(".submenu").addClass(
    ($(".submenu ul").size() == 2) ? "two-col" : "three-col"
  );
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号