简体   繁体   English

甚至在单击某个项目时,如何获取该项目的索引?

[英]How do I get the index of a list item during a click even on that item?

How do I get the index of a list item during a click even on that item? 甚至在单击某个项目时,如何获取该项目的索引?

<ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
<ul>

$('li').live('click', function(e){
    alert(e.index);
})

Thanks 谢谢

$(document).delegate('li', 'click', function () {
    alert($(this)).index());
});

Here is a demo: http://jsfiddle.net/xERTx/1/ 这是一个演示: http : //jsfiddle.net/xERTx/1/

.live() is now depreciated as of jQuery 1.7 in favor of .on() and .delegate() (source: http://api.jquery.com/live ). 现在,从jQuery 1.7开始, .live()被贬值,转而支持.on().delegate() (来源: http : .delegate() )。

Have you tried the index keyword? 您是否尝试过index关键字?

http://api.jquery.com/index/ http://api.jquery.com/index/

试试这个

$(e.target).index( $(e.target).parent() );

solution here 解决方案在这里

http://jsfiddle.net/SMpWs/1/ http://jsfiddle.net/SMpWs/1/

$('li').click(function() {
    alert($(this).index());
}); 

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

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