[英]jQuery: How to determine the index of <li> element in an unordered list when clicked?
I want to know the index of the clicked li element in the unordered list. 我想知道无序列表中单击的li元素的索引。 How to achieve this with jQuery?
如何用jQuery实现呢?
$('ul li').on('click', function(ev) {
var index = $(this).index();
});
$( 'li' ).on( 'click', function() {
$( this ).index()
} )
this ? 这个 ? http://api.jquery.com/index/
http://api.jquery.com/index/
<head>
<style>
div { background:yellow; margin:5px; }
span { color:red; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<span>Click a div!</span>
<div>First div</div>
<div>Second div</div>
<div>Third div</div>
<script>
$("div").click(function () {
// this is the dom element clicked
var index = $("div").index(this);
$("span").text("That was div index #" + index);
});
</script>
</body>
</html>
code from Jquery Api 来自Jquery Api的代码
Just do this way:- 就是这样:-
$('li').click(function() {
alert($(this).index());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.