jQuery追加到列表的顶部/顶部


105

我有这个无序列表

<ul>
   <li>two</li>
   <li>three</li>
</ul>

有什么办法可以使我排在无序列表之前,这样它就可以像这样结束吗?

<ul>
   <li>ONE</li>
   <li>two</li>
   <li>three</li>
</ul>

注意,“ ONE”已添加到列表的FRONT / TOP中。

jquery 

Answers:


249
$("ul").prepend("<li>ONE</li>");

38

像这样简单的事情应该起作用:

$("ul").prepend("<li>ONE</li>");

7

使用追加的prepend instread

<h2>Greetings</h2>    
<div class="container">
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>

$('.container').prepend('<p>Test</p>');

请参阅http://api.jquery.com/prepend/了解更多信息。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.