简体   繁体   English

document.querySelectorAll()函数不适用于所有元素

[英]document.querySelectorAll() function not working on all elements

I am using document.querySelectorAll() function to catch <a> in my list of <li> but the function is not working on all elements i have to define like this document.querySelectorAll('//elements')[0] , document.querySelectorAll('//elements')[1] if i define like this it works on element on that index only. 我正在使用document.querySelectorAll()函数在<li>列表中捕获<a> ,但是该函数不适用于我必须像document.querySelectorAll('//elements')[0]这样定义的所有元素document.querySelectorAll('//elements')[1]如果我这样定义,它仅适用于该索引上的元素。

I don't think its good to define like this for all elements. 我认为对所有元素进行这样的定义不是很好。 Can anyone tell me please where i am wrong why its not working on all elements. 谁能告诉我我错了,为什么它不能在所有元素上起作用。

Here is my current code 这是我当前的代码

HTML 的HTML

<ul class="questions">
    <li>
        <a href="#" class="clearfix"><span class="faq_name">My Account</span><span class="arrow"><img src="images/plus.png"></span></a>
        <div class="answer_box">
            <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. </p>
        </div>
    </li>
    <li>
        <a href="#" class="clearfix"><span class="faq_name">Categories</span><span class="arrow"><img src="images/plus.png"></span></a>
        <div class="answer_box">
            <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. </p>
        </div>
    </li>
    <li>
        <a href="#" class="clearfix"><span class="faq_name">Trouble with check-in</span><span class="arrow"><img src="images/plus.png"></span></a>
        <div class="answer_box">
            <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. </p>
        </div>
    </li>
</ul>

JS JS

document.querySelectorAll('.questions li a').onclick = function() {myFunction()};
function myFunction(){
    alert('asd');
}; 

querySelectorAll returns a collection of elements. querySelectorAll返回元素的集合 The DOM doesn't have a "set-based" feature letting you assign handlers to the entire set in one statement (the jQuery library does, if you fancy using it), you have to use a loop. DOM没有“基于集合”的功能,让您可以在一个语句中将处理程序分配给整个集合(如果您愿意使用jQuery库,则可以使用一个循环)。

For example: 例如:

Array.prototype.forEach.call(
    document.querySelectorAll('.questions li a'),
    function(element) {
        element.onclick = myFunction;
    }
);
function myFunction(){
    alert('asd');
}

There, we use Array#forEach to loop through the collection (because it can be used on any array-like object, not just arrays). 在那里,我们使用Array#forEach遍历集合(因为它可以用于任何类似数组的对象,而不仅仅是数组)。 You have several other options outlined in the "array-like" part of this answer . 您还可以在此答案的“类似数组”部分中概述其他几个选项。


Side note 1: I used MyFunction directly above since wrapping it in a function that just calls it seems unnecessary, but that will change what arguments it receives, so add a wrapper if needed. 旁注1:我在上面直接使用了MyFunction ,因为将它包装在一个仅调用它的函数中似乎没有必要,但这会更改它接收的参数,因此,如果需要,可以添加包装器。

Side note 2: While I didn't change it above, I strongly recommend using addEventListener (and attachEvent if you have to support IE8 or the broken "compatibility mode" of IE9-IE11) instead of onclick . 旁注2:虽然我在上面没有做任何更改,但我强烈建议使用addEventListener (如果必须支持IE8或IE9-IE11的“兼容模式”,请使用attachEvent )而不是onclick

Side note 3: For this specific use case, you might look into event delegation rather than adding a handler to each element. 附注3:对于此特定用例,您可以研究事件委托,而不是向每个元素添加处理程序。 In event delegation, you add a handler to a container that has all your links in it (could be document.body , but usually there's a container closer to them that's more appropriate), and then when the event occurs, you see if it passed through a matching element while bubbling. 在事件委托中,您将一个处理程序添加到一个包含所有链接的容器中(可以是document.body ,但是通常更靠近它们的容器更合适),然后在事件发生时查看事件是否通过冒泡时通过匹配的元素。

Side note 4: On those occasions where you just want the first match, intead of querySelectorAll(...)[0] , use querySelector(...) . 旁注4:在您只希望第一个匹配的情况下,即querySelectorAll(...)[0] ,请使用querySelector(...) It returns the first match, or null if nothing matches. 它返回第一个匹配项;如果没有匹配项,则返回null

You need to convert the "HTMLcollection" to an array then loop that array to bind event listeners to each element. 您需要将“ HTMLcollection”转换为数组,然后循环该数组以将事件侦听器绑定到每个元素。

By "calling" Array.prototype.slice on a 'array-like-objects', slice iterates & fills an new array that is returned. 通过在"calling"类似数组的对象”上"calling" Array.prototype.slice, slice迭代并填充返回的新数组。 Then you can use array methods on that array. 然后,您可以在该数组上使用数组方法。

 Array.prototype.slice.call(document.querySelectorAll('.questions li a')) //converts HTMLCollection to an array //or Array.apply(null ,document.querySelectorAll('.questions li a')) .forEach(function(element){ element.onclick = function(){ //do something } }) 

I found an easier way with a loop in which loop will run till the length of the elements 我发现了一个更简单的循环方法,其中循环将一直运行到元素的长度为止

var question = document.querySelectorAll('.questions li a');
var i;
for(i=0;i<question.length;i++){
    question[i].addEventListener('click',myFunction)
}
function myFunction(){
    alert('adasd');
}

Every thing is same just calling the click in loop. 每件事都是一样的,只是调用click in循环。

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

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