开发者

jquery count li's in top level UL

开发者 https://www.devze.com 2023-01-16 19:05 出处:网络
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 :(

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;
0

精彩评论

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

关注公众号