简体   繁体   中英

Jquery get Index() of appended li

I have a jquery append() to a ul function that I am trying to return the index() of the new li . After that I will have the ability to drag and drop the li into a different position and I need the index of that too.

How can I accomplish this?

u can use index function in jquery

http://jsfiddle.net/N4SDw/

$('button').click(function(){
$('ul').append('<li>im list</li>');
 var list =  $('li:last').index();

    alert(list+1)
})

After you append use jquery lenght to get size of your list http://api.jquery.com/length/

Substract 1 from length to get index of your appended li

This works for me:

$("<li>Test</li>").appendTo($("#test")).index();

JSFiddle: http://jsfiddle.net/3uqJT/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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