I want to know the index of the clicked li element in the unordered list. How to achieve this with jQuery?
$('ul li').on('click', function(ev) {
var index = $(this).index();
});
$( 'li' ).on( 'click', function() {
$( this ).index()
} )
this ? 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
Just do this way:-
$('li').click(function() {
alert($(this).index());
});
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.