简体   繁体   English

点击时如何使用 Jquery 定位所有元素

[英]How to target all elements with Jquery on click

Only first element getting clicked not all, how to get alert on all elements.只有第一个元素被点击而不是全部,如何获得所有元素的警报。

Jsfiddle提琴手

 var btn = document.querySelector(".box i"); btn.onclick = function () { alert('hello'); }
 <div class="box"> <i class="fa fa-address-book-o" aria-hidden="true"></i> <i class="fa fa-address-book-o" aria-hidden="true"></i> <i class="fa fa-address-book-o" aria-hidden="true"></i> <i class="fa fa-address-book-o" aria-hidden="true"></i> </div>

Try querySelectorAll for select all then attract onclick to all element尝试 querySelectorAll for select all 然后将 onclick 吸引到所有元素

 var btnlist = document.querySelectorAll(".box i"); btnlist.forEach(element =>{element.onclick = function () { alert('hello'); }});
 <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <div class="box"> <i class="fa fa-address-book-o" aria-hidden="true"></i> <i class="fa fa-address-book-o" aria-hidden="true"></i> <i class="fa fa-address-book-o" aria-hidden="true"></i> <i class="fa fa-address-book-o" aria-hidden="true"></i> </div>

Despite the title of the question, you don't currently use jQuery in your code.尽管有问题的标题,但您目前没有在代码中使用 jQuery 。 So, I provide 2 solutions:所以,我提供了2个解决方案:

jQuery : jQuery

jQuery('.box i').click(function() {
    alert('hello');
});

Pure JS纯JS

window.addEventListener('click', function(e) {
    // or e.target.classList.contains('fa-address-book-o')
    if (e.target.className === 'fa fa-address-book-o') { // Better to add some additional class to identify element, like "fireAlert" for instance.
        alert('hello');
    }
});

Alternatively, you could select icons with query selector and add individual event listeners to all of them in a loop, but it's bad for performance & generally not considered a good thing to do.或者,您可以使用带有查询选择器的 select 图标并在循环中向所有这些图标添加单独的事件侦听器,但这对性能不利并且通常不被认为是一件好事。

The reason why it only work on the first element is that using .querySelector() method will select only the first element the meet your selector criteria, to be able to select all the elements you have to use .querySelectorAll() which will return a NodeList which can iterate through like this:它仅适用于第一个元素的原因是使用.querySelector()方法将 select 只有第一个元素满足您的选择器条件,才能 select 您必须使用的所有元素.querySelectorAll()将返回一个NodeList可以像这样迭代:

 var btns = document.querySelectorAll(".box i"); btns.forEach((el, i) => { el.onclick = function () { console.log(`Hello; ${i}`); } });
 <div class="box"> <i class="fa fa-address-book-o" aria-hidden="true">1</i> <i class="fa fa-address-book-o" aria-hidden="true">2</i> <i class="fa fa-address-book-o" aria-hidden="true">4</i> <i class="fa fa-address-book-o" aria-hidden="true">5</i> </div>

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

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