[英]Add class to the clicked div with multiple divs with same class
How do I get the clicked div if the divs both got same class. 如果两个div都具有相同的类,如何获取点击的div。 For an example:
例如:
<div class="box"></div> <div class="box"></div>
And I want to add another class to the div I click on. 我想向我单击的div添加另一个类。
New at JavaScript but I have figured out that I should use "this" in some way. JavaScript的新功能,但我发现我应该以某种方式使用“ this”。
What I have done so far: 到目前为止,我所做的是:
box = document.getElementById("box");
box.addEventListener("click, function(event) {
event.target.classList.toggle("clicked");
}
But of course this only works for a div with an Id and not for multiple divs. 但是,当然,这仅适用于具有ID的div,不适用于多个div。
You can use document.querySelectorAll
here like: 您可以在此处使用
document.querySelectorAll
:
var boxes = document.querySelectorAll('.box');
Array.from(boxes).forEach(box => {
box.addEventListener('click', function(e) {
e.target.classList.toggle('clicked');
});
});
with jquery 用jQuery
$('.box').click(function() { console.log($(this).text()); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box">one</div><div class="box">two</div>
If you want to use document.getElementById()
you need to add element the id
attribute: 如果要使用
document.getElementById()
,则需要添加元素id
属性:
<div id="box1" class="box">
By design, ids are meant to be unique, so if you can assign each element an id, it shoud be enough. 按照设计,ID的含义是唯一的,因此,如果您可以为每个元素分配一个ID,则应该足够。
I'd suggest an approach where you add the listener on a parent (or whole document) and check for the clicked element inside: 我建议一种方法,将侦听器添加到父级(或整个文档)上,然后检查其中的clicked元素:
document.addEventListener('click', function (event) {
if(event.target.classList.contains('box')) {
event.target.classList.toggle('clicked');
}
}
This approach is better performance-wise as it creates only one event handler, not a handler per element. 这种方法在性能上更好,因为它只创建一个事件处理程序,而不是每个元素创建一个处理程序。
If you attach the eventListener onto a containing element, here I've just used body, but more targeted selectors can give better performance. 如果将eventListener附加到一个包含元素上,则这里我只是使用body,但是更有针对性的选择器可以提供更好的性能。
You could then check the class of the item clicked. 然后,您可以检查所单击项目的类别。
Example below.. 下面的例子
document.body.addEventListener("click", function (evt) { var el = evt.srcElement; if (el.classList.contains("box")) el.classList.toggle("clicked"); });
.clicked { background-color: yellow; }
<div class="box">Box 1</div> <div class="notabox">Not a Box</div> <div class="box">Box 2</div>
If you want to keep using pure JavaScript without any additional lib, I believe Query Selectores should help you ( https://developer.mozilla.org/pt-BR/docs/Web/API/Element/querySelectorAll ). 如果您想继续使用纯JavaScript而没有任何其他库,我相信Query Selectores应该可以为您提供帮助( https://developer.mozilla.org/pt-BR/docs/Web/API/Element/querySelectorAll )。
Evolving your snippet: 不断发展的代码段:
document.querySelectorAll("box").forEach( function (){
this.addEventListener('click', function(event) {
event.target.classList.toggle("clicked");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.