开发者

jquery and xml. Getting nodes as list items

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜