简体   繁体   中英

Use jQuery to find out how many list items are in an unordered list

I am creating a tabbed content interface. The content panels are an unordered list created using a server-side script. I want to add tabs via jQuery to control the panels. The only requirement for the jQuery plugin to work is to have the same amount of panels as tabs (the number of panels isn't always consistent). I need to query how many list items are in my ul.panels and store it in a variable, so I can create a loop with that variable to append my list items in ul.tabs.

I don't have access to the server-side script, so modifying the PHP isn't a possibility.

Like this?

 var cnt = $('ul.panels li').length

jQuery hangs varous things off of it so you don't really need your own loop (methinks). You could do this too:

 $('ul.panels li').each( function(idx, panel) {
     // stir in special sauce
 });
var count = $("ul.panels li").size();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM