简体   繁体   中英

add li to existing ul using jquery

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,

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>"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM