最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create unordered list dynamically using jQuery - Stack Overflow

programmeradmin4浏览0评论

How can I create an unordered list dynamically using jQuery? I read the image file path (href and src) from an XML file.

 <ul>
       <li><a href="images/test1.png"><img id="imageSlide" src="images/test1.png" alt="" /></a></li>
       <li><a href="images/test2.png"><img id="imageSlide" src="images/test2.png" alt="" /></a></li>
 </ul>

It should create unordered list based on number of XML nodes in the XML file.

How can I create an unordered list dynamically using jQuery? I read the image file path (href and src) from an XML file.

 <ul>
       <li><a href="images/test1.png"><img id="imageSlide" src="images/test1.png" alt="" /></a></li>
       <li><a href="images/test2.png"><img id="imageSlide" src="images/test2.png" alt="" /></a></li>
 </ul>

It should create unordered list based on number of XML nodes in the XML file.

Share Improve this question edited Oct 2, 2012 at 12:21 user2428118 8,1244 gold badges46 silver badges73 bronze badges asked Aug 18, 2010 at 6:27 SAKSAK 3,8987 gold badges29 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Well, you have to loop over your XML structure and create new LI nodes in the body of that.

var dummyXML = "<foo><dummy>element</dummy><dummy>element</dummy><dummy>element</dummy></foo>";
var HTMLmarkup = '';

$(dummyXML).find('dummy').each(function(){
   HTMLmarkup += '<li>' + $(this).text() + '</li>';
});

$('ul').append(HTMLmarkup);

That of course, is just a dummy example. Infact you should consider to use more sophisticated XML traversal systems like XPath (depending on how big your XML file is).

$('ul li').text(function(index) {
  return '<a href=images/test' +index +'><img id="imageSlide" src="images/test'+index+'.png" alt="" /></a>';
});
发布评论

评论列表(0)

  1. 暂无评论