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