简体   繁体   English

使用jquery将li添加到现有ul

[英]add li to existing ul using jquery

I have 2 divs nested with a ul inside them. 我有2个div嵌套在他们里面的ul。

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

I wanted to add a li dynamically to the ul using jquery. 我想使用jquery动态地将一个li添加到ul。 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 您只需要定义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!'}); 或者正如PSL指出的那样,只是: var li = $('<li>', {'html':'Test!'});

Demo 演示

if you want to append a li element to the end of your ul: 如果你想将一个li元素附加到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 或者如果你想在你的ul的开头添加一个li

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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