简体   繁体   English

当所有元素具有相同的类名时,如何获取被点击元素的id?

[英]How to get id of the clicked element when all of the elements have the same class name?

My code would be like this 我的代码就像这样

<select id="id_1" class="editable-input" name="name_1">

<select id="id_2" class="editable-input" name="name_2">

<select id="id_3" class="editable-input" name="name_3">

How do I know which one is being clicked and how to get the id and name of the clicked element? 我如何知道哪一个被点击以及如何获取被点击元素的ID和名称?

PS: I want to write the code in javascript later, because I am gonna put that code to GTM variable PS:我想稍后用javascript编写代码,因为我要将代码放到GTM变量中

With Plain JS + html update: 使用Plain JS + html更新:

 var info = function(dd) { alert(dd.id + ", " + dd.name); }; 
 <select id="id_1" class="editable-input" name="name_1" onclick="info(this);"></select> <select id="id_2" class="editable-input" name="name_2" onclick="info(this);"></select> <select id="id_3" class="editable-input" name="name_3" onclick="info(this);"></select> 

With Plain JS: 使用Plain JS:

 var info = function(dd) { return function() { alert(dd.id + ", " + dd.name); } }; var dds = document.getElementsByClassName("editable-input"); for (var i = 0, l = dds.length; l > i; i++) dds[i].onclick = info(dds[i]); 
 <select id="id_1" class="editable-input" name="name_1"></select> <select id="id_2" class="editable-input" name="name_2"></select> <select id="id_3" class="editable-input" name="name_3"></select> 

With jQuery: 使用jQuery:

 $(function() { $('select.editable-input').on('click', function(e) { alert(this.id + ", " + this.name); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <select id="id_1" class="editable-input" name="name_1"></select> <select id="id_2" class="editable-input" name="name_2"></select> <select id="id_3" class="editable-input" name="name_3"></select> 

If you are using jQuery, you can use the following: 如果您使用的是jQuery,则可以使用以下命令:

 $(".editable-input").on("click", function(e) {
    var sender = $(this);

    console.log(sender.prop("id"));
    //OR
    console.log(this.id);
 });

if you are using JavaScript: 如果您使用的是JavaScript:

 var items = document.getElementsByClassName("example"),
     currentItem;

 for(var i = items.length; i--;) {
   currentItem = items[i];
   currentItem.onclick = function(e) {
      console.log(this.id);
   };
 }

There are some ways of doing that, but the best way of doing it, is by delegating one single event to the parent of your children. 有一些方法可以做到这一点,但最好的方法是将一个单独的事件委托给孩子的父母。

 var div = document.getElementById("parent"); div.addEventListener("click", function(e) { var el = e.target; // here you can get el.id and el.name and do whatever you want if (el.tagName.toLowerCase() === "select") alert(el.id); }); 
 <div id="parent"> <select id="id_1" class="editable-input" name="name_1"></select> <select id="id_2" class="editable-input" name="name_2"></select> <select id="id_3" class="editable-input" name="name_3"></select> </div> 

Doing that, you create only one event that could handle all the children you have inside the div. 这样做,您只创建一个可以处理div中所有孩子的事件。 So, you'll use less memory, and even if you create another <select> dinamically, it would already have the event delegated to it. 因此,您将使用更少的内存,即使您创建了另一个<select> ,但它已经将事件委托给它。

Know more about event delegation . 了解有关事件委派的更多信


And if you don't like that approach, you can always use the conventional one: 如果您不喜欢这种方法,您可以随时使用传统方法:

var sels = document.querySelecorAll(".editable-content");
for (var i = 0; i < sels.length; i++) {
  sels[i].addEventListener("click", function(e) {
    var el = e.target;
    // here you can get el.id and el.name and do whatever you want
  });
}

暂无
暂无

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

相关问题 当这些元素都具有相同的 ID 时,有没有办法只删除单击的元素? - Is there a way to delete only clicked element when those elements all have the same ID? 获取被单击元素的ID名称,并显示具有相同类名称的div - Get ID name of a clicked element and show divs with the same name of class 如何获取被单击的元素的类名并使用相同的类名来操纵其他元素? - How to get the class name of the element that was clicked on and use that same class name to manipulate the other elements? Jquery:获取一个类的所有元素,这些元素不是具有相同类名的元素的后代? - Jquery: Get all elements of a class that are not decendents of an element with the same class name? 当多个元素具有相同的名称时,无法在成功返回 Ajax 时定位点击的元素 - unable to target clicked element on successful Ajax return, when multiple elements have same name 在单击时,仅将类应用于单击的元素,而不是将所有元素都应用于同一类 - On click only apply class to element clicked, not all elements with the same class 我怎样才能通过同一个班级的班级名称获取所有元素? - How i could get all element by class name of same class? 如何通过 class 名称仅获取单击的元素? - How to get only clicked element by class name? 当您在同一页面中具有多个具有相同类别的元素时,如何通过一个类别选择ID - How to select an id through a class when you have multiple elements in the same page that have same class 获取被点击元素的ID名称 - Get ID name of a clicked element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM