简体   繁体   English

jQuery:如何确定索引 <li> 单击后将其置于无序列表中?

[英]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.

相关问题 如何把检索到的<li>使用 JQuery 的另一个无序列表末尾的无序列表元素? - How to put the retrieved <li> element of an unordered list at the end of another unordered list using JQuery? 如何获得 <li> 当里面的元素被点击时的元素? - How to get index of <li> element when element inside it clicked? 使用 JQuery 单击 li 时如何获取内部元素的值 - How to get a value of inner element when clicked on li using JQuery jQuery - 在无序列表中获取spcific <li>的数字位置/索引 - jQuery - Get numeric position/index of spcific <li> in an unordered list 当单击lielment时,如何更改jQuery中li元素上的活动类并将用户重定向到指定页面? - How to change active class on li element in JQuery and redirect the user to the specified page when li elelment is clicked? 如何对我的无序列表元素旁边的按钮进行编程以在单击时删除其关联列表元素? - How to program the buttons beside my unordered list elements to delete its associate list element when clicked? 单击时如何选择li元素? - How to select the li element when clicked? 如何在输入时单击 select li 元素 - How to select the li element when clicked with on input 具体造型 <li> 无序列表中的元素 <ul> - Styling specific <li> element in unordered list <ul> 如何使用 JavaScript 在单击时将 CSS styles 添加到列表元素,但在单击另一个 li 项目时将其删除? - How can I use JavaScript to add CSS styles to a list element when clicked, but remove it when another li item is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM