开发者

Traversing an unordered list using jQuery

开发者 https://www.devze.com 2022-12-12 01:37 出处:网络
This should be an easy one, but I\'m not find开发者_StackOverflowing much online: I have an unordered list <ul>, with a few list items underneath it <li>, and I\'d like to address each on

This should be an easy one, but I'm not find开发者_StackOverflowing much online:

I have an unordered list <ul>, with a few list items underneath it <li>, and I'd like to address each one in the list, and act on it. How can I do this using jQuery?

Thanks.


You could use each() for this.

E.g.

$('ul#id li').each(function(index, element) {
    var li = $(element);
    // ...
});


Something close to this:

$('#myulid').children('li').each(function(i, n) { alert($(this).html()); });


Use this code and change id as appropriate.

$("ul#id_of_desired_ul > li").each(function() {
   //do stuff
   // $(this) references each li
});
0

精彩评论

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