简体   繁体   English

querySelectorAll使用this.element时仅选择第二个元素

[英]querySelectorAll only selecting second element when using this.element

I've recently started learning about JavaScript objects, and how frameworks such as jQuery , and Modernizr work. 我最近开始学习JavaScript对象,以及诸如jQueryModernizr之类的框架的工作方式。

I have tried to create my own little "framework" , to further learn how JavaScript objects work, and how to utilize them to their full potential. 我试图创建自己的小“框架” ,以进一步学习JavaScript对象如何工作,以及如何充分利用它们。

It's all gone pretty smoothly so far, until I tried to set a global variable using the querySelectorAll() method (and a for loop) , to grab multiple elements with the specified selector. 到目前为止,一切都很顺利,直到我尝试使用querySelectorAll()方法(和for循环)设置全局变量,以使用指定的选择器捕获多个元素。

With this, I intended to add or remove a class from each of those elements with that specific selector. 因此,我打算使用该特定选择器从每个元素中添加或删除一个class However, it only ever worked on the very last element of the bunch. 但是,它只适用于该组中的最后一个元素。


Here is my (relevant) JavaScript : 这是我的(相关) JavaScript

var aj = function(sr){
    this.selector = sr || null; // set global selector variable
    this.element = null;
}

aj.prototype.init = function(){
    switch(this.selector[0]){
        // first, second, third case e.t.c...
        default:
            var els = document.querySelectorAll(this.selector); // select all elements with specified selector (set above)
            for(var i = 0; i < els.length; i++){
                this.element = els[i];
            }
    }
};

aj.prototype.class = function(type, classes){
    if(type === "add"){ // if the user wants to add a class
        if((" " + this.element.className + " ").indexOf(" " + classes + " ") < 0){
            this.element.className += " " + classes;
        }
    } else if(type === "remove") { // if the want to remove a class
        var regex = new RegExp("(^| )" + classes + "($| )", "g");

        this.element.className = this.element.className.replace(regex, " ");
    }
};

Example : 范例

<div class="example-class">Example</div>
<div class="example-class">Example 2</div> <!-- only this one will be altered !-->
<script>
    $(".example-class").class("add", "classname");
</script>

Why would this be occurring? 为什么会这样呢? My for loop appears to be correct, so I am unsure what is wrong. 我的for循环似乎是正确的,因此我不确定出什么问题。 Apologies if it appears pretty obvious, however, I'm still new to vanilla JavaScript . 道歉,如果它看起来很明显,但是,我还是香草JavaScript新手。

All help (and suggestions) is appreciated, 感谢所有帮助(和建议),
Thanks. 谢谢。

 for(var i = 0; i < els.length; i++){ this.element = els[i]; } 

You have a loop. 你有一个循环。 Each time it goes around the loop it assigns a value to this.element . 每次绕过循环时,都会为this.element分配一个值。

The first time it goes around the loop it assigns the value of els[0] . 第一次绕过循环时,它将分配els[0]的值。 The second time it assigns the value of els[1] . 第二次分配els[1]的值。

Since you only have two elements that match, it reaches the end of the loop and stops. 因为只有两个匹配的元素,所以它到达循环的结尾并停止。

At this point, this.element is still equal to els[1] . 此时, this.element仍然等于els[1]


If you want to do something (like add membership of a class) to each item in els then you have to loop over els at the time you modify className . 如果要对els每个项目执行某些操作(例如添加类的成员身份),则必须在修改className遍历els

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

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