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

javascript - add li to existing ul using jquery - Stack Overflow

programmeradmin7浏览0评论

I have 2 divs nested with a ul inside them.

<div id="slide1">
    <div class="images">
         <ul>
         </ul>
     </div>
</div>

I wanted to add a li dynamically to the ul using jquery. The selector I am using is..

$("#slide1 > div.images ul").append(

It does not work. Where am I wrong?

Thanks a lot in advance,

I have 2 divs nested with a ul inside them.

<div id="slide1">
    <div class="images">
         <ul>
         </ul>
     </div>
</div>

I wanted to add a li dynamically to the ul using jquery. The selector I am using is..

$("#slide1 > div.images ul").append(

It does not work. Where am I wrong?

Thanks a lot in advance,

Share Improve this question asked Dec 24, 2013 at 21:51 pessipessi 6791 gold badge11 silver badges26 bronze badges 3
  • 4 That's the correct selector. Maybe the problem is with the LI you're trying to add. Show the rest of the code. – Barmar Commented Dec 24, 2013 at 21:54
  • the selector is right, what is the code after append? – Daiwei Commented Dec 24, 2013 at 22:01
  • silly mistake on my part..with variable name..works now. – pessi Commented Dec 24, 2013 at 22:09
Add a ment  | 

2 Answers 2

Reset to default 4

You code should work you just need to define the li

var li = $('<li></li>');
li.html('Test!');
$("#slide1 > div.images ul").append(li);

Or as PSL pointed out, just: var li = $('<li>', {'html':'Test!'});

Demo

if you want to append a li element to the end of your ul:

$("#slide1 > div.images ul").append($("<li>my new li</li>"));

or if you want to add a li to the beginning of your ul

$("#slide1 > div.images ul").prepend($("<li>my new li</li>"));
发布评论

评论列表(0)

  1. 暂无评论