jquery count li's in top level UL
I need to count the amount of LI's in a top level UL.
My top level menu has 6 items in it but this could dynamically change. I thought this could work but it is still counting the child li's too :(
var numTopNavItems = 0;
$("ul.rmHorizontal > li").each(f开发者_如何学Pythonunction (i) {
numTopNavItems += i;
alert("numTopNavItems = " + numTopNavItems);
});
Any ideas why this might be?
Thanks, James
The proper way to count the number of matched element is
var numTopNavItems = $("ul.rmHorizontal > li").length;
alert("numTopNavItems = " + numTopNavItems);
The method should work as long as only the top level <ul>
has class rmHorizontal
. If not, try the selector
"ul.rmHorizontal:first > li"
instead.
Got it thanks:
var count = $("#myList").children().length;
精彩评论