开发者

Adding a new element before the top element using jquery [duplicate]

开发者 https://www.devze.com 2023-03-29 19:56 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: jQuery: how to add <li> in an existing <ul>?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

jQuery: how to add <li> in an existing <ul>?

I have a <UL> which contains a list of <LI>. What I am looking at is on an event to add a new <LI> item as the first element on the <UL>. Can you please advice on how I can acheive this using jquery.

Example:

<UL>
   <LI>First Item</LI>
   <LI>Second Item</LI>
   <LI>Third Item</LI>
   <LI>Fourth Item</LI>
</UL>

on click of a button, i will need to add a new LI element as the first element in the UL, which would look like the below

<UL>
   <LI>New Item is here</LI>
   <LI>First Item</LI>
   <LI>Second Item</LI>
   <LI>Third Item</LI>
   &l开发者_StackOverflowt;LI>Fourth Item</LI>
</UL>


$("ul").prepend("<li>New Item is here</li>");


<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script language="javascript">
function example_append() {
   $("ul").prepend("<li>New Item is here</li>");
}
</script>
<input type="button" value="Append" onclick="example_append()" /><br/>
<ul>
   <LI>First Item</LI>
   <LI>Second Item</LI>
   <LI>Third Item</LI>
   <LI>Fourth Item</LI>
</ul>

Live Example @ http://jsfiddle.net/XNyJz/2/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号