jquery 追加到列表的前面/顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/469640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:12:07  来源:igfitidea点击:

jquery append to front/top of list

jquery

提问by dasony

I have this unordered list

我有这个无序列表

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

Is there a way I can prepend to the unordered list so that it ends up like this?

有没有办法可以在无序列表的前面加上这样的结果?

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

Notice the "ONE" is added to the FRONT/TOP of the list.

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

回答by dasony

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

回答by nezroy

Something simple like this ought to work:

像这样简单的事情应该可以工作:

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

回答by Ishan Liyanage

use prepend instread of append

使用 prepend instread of append

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

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

refer http://api.jquery.com/prepend/for more info.

有关更多信息,请参阅http://api.jquery.com/prepend/