简体   繁体   English

哪一个 <li> 点击了元素?

[英]which <li> element was clicked?

I have 5 (maybe more) li elements. 我有5个(可能更多)li元素。

<ul>
 <li>One</li>
 <li>Two</li>
 <li>Three</li>
 <li>Four</li>
 <li>Five</li>
</ul>

I want to get which elements was clicked( which row?? ). 我想得到哪些元素被点击( 哪一行? )。 If random user clicks Two I want to get $("li:eq(1)") (as typed). 如果随机用户点击两个我想得到$("li:eq(1)") (如键入)。

How can I get this result? 我怎样才能得到这个结果?

You can use jQuery.index . 您可以使用jQuery.index Something like this: 像这样的东西:

$('ul > li').click(function() {
   alert($(this).index($(this).parent('li'));
});
$("#ulId li").click(function() {
   $(this).something(); //the clicked li is $(this), and you can invoke functions on it.
})

You can get the text node value of the clicked item with: 您可以使用以下命令获取所单击项目的文本节点值:

$('li').click(function(){
   var clicked = $(this).text();

  alert(clicked+" was clicked");
});

Working example: http://jsfiddle.net/tbugV/1/ 工作示例: http//jsfiddle.net/tbugV/1/

$("#mylist li").each(function(index)
                     {
                         $(this).data("row", index);
                     }).
                click(function() 
                      {
                        alert($(this).data("row"));   
                      });

If you give your elements an id such as 如果你给你的元素一个id,如

<ul id="mylist">
 <li id="el_1">One</li>
 <li id="el_2">Two</li>
 <li id="el_3">Three</li>
 <li id="el_4">Four</li>
 <li id="el_5">Five</li>
</ul>

Then you can use $(this).attr(id) in the click handler to determine the id of the clicked element. 然后,您可以在click处理程序中使用$(this).attr(id)来确定单击元素的ID。 This will also allow to give non sequential ids to your elements, and will detach what's written in the <li> from the actual value you get. 这也将允许为元素提供非顺序ID,并将从您获得的实际值中分离<li>中的内容。 Also, you can encode multiple value in the id (for instance el_5_3 ) which can be useful sometimes. 此外,您可以在id中编码多个值(例如el_5_3 ),这有时可能很有用。

$("#mylist li").click(function()
                      {
                      var id = $(this).attr("id").split("_");
                      alert("You clicked the element with id="+id[1]);    
                      });

Working example: http://jsfiddle.net/jFrdp/ 工作示例: http//jsfiddle.net/jFrdp/

 $('html').click(function() {
     var el = e.target;    
     alert(el);
 });

As people just keep posting code, and no explanations, I will try the other way around... 由于人们只是不断发布代码,也没有解释,我会尝试相反的方式......

The click event handler is called in the scope of the clicked element, so you can use the this keyword to access the element. click事件处理程序在clicked元素的范围内调用,因此您可以使用this关键字来访问该元素。 You can use $(this) to get a jQuery object that contains the clicked element, so that you can use jQuery methods on it. 您可以使用$(this)来获取包含被点击元素的jQuery对象,以便您可以在其上使用jQuery方法。

Example: 例:

$(document).ready(function(){

  $('ul li').click(function({
    var text = $(this).text();
    alert('You clicked on the item with the text "' + text + '"');
  }));

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

This code will alert one when user will click the one button. 当用户单击一个按钮时,此代码将发出警报。

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

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