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"
);
});
精彩评论