append要将dom添加为目标中的最后一个元素,但如何将其添加为第一个?

Fre*_*ood 0 jquery

我想首先添加元素,如下所示.我怎样才能做到这一点 ?

$('<li>').appendTo($('ul')); // add it as last element

<ul>
    <!-- i want to add element here -->
    <li></li>
    <li></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

emc*_*lle 6

使用jQuery的.prepend()http://api.jquery.com/prepend/

$('ul').prepend('<li>new</li>')
Run Code Online (Sandbox Code Playgroud)