简体   繁体   English

jQuery获取附加li的Index()

[英]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 . 我对ul函数有一个jquery append() ,试图返回新liindex() 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. 之后,我将能够将li拖放到其他位置,我也需要该索引。

How can I accomplish this? 我该怎么做?

u can use index function in jquery 你可以在jQuery中使用index功能

http://jsfiddle.net/N4SDw/ 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/ 追加后,使用jength长度获取列表的大小http://api.jquery.com/length/

Substract 1 from length to get index of your appended li 从长度中减去1得到您所附加li的索引

This works for me: 这对我有用:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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