开发者

jquery and xml. Getting nodes as list items

开发者 https://www.devze.com 2023-03-05 11:29 出处:网络
I am trying to parse some xml and most of it is fine. However, I am having trouble getting nodes into and creating list items. my code so far is something like

I am trying to parse some xml and most of it is fine. However, I am having trouble getting nodes into and creating list items. my code so far is something like

$(xml).find('section1').each(fu开发者_如何转开发nction (i) {
   var myLink = $(xml).find('link').text();
   $('#set1').find('ul').eq(i).append("<li>"+myLink+"</li>");
});

but what happens is it takes all of the "myLinks" nodes and puts them in one <li>. Any ideas on crating an <li> for each myLink node?

Thanks


Maybe you need to change $(xml) to $(this) inside your loop?

And fixup the append code (you only have one list, right? if so, remove the .eq stuff) like this:

var $list = $('#set1').find('ul');

$(xml).find('section1').each(function () {
   var myLink = $(this).find('link').text();
   $list.append("<li>"+myLink+"</li>");
});

If that works, you might be able to simplify it down to this:

var $list = $('#set1').find('ul');

$(xml).find('section1 link').each(function () {
   var myLink = $(this).text();
   $list.append("<li>"+myLink+"</li>");
});

And perhaps even further using $.map

0

精彩评论

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