简体   繁体   English

遍历DOM元素

[英]Iterating over DOM element

I am iterating DOM elements using a for loop, using 2 syntax and in both I am getting different results. 我正在使用for循环,使用2语法来迭代DOM元素,并且在这两种方法中我都得到了不同的结果。

The JavaScript method 1 is JavaScript方法1是

for (var sortable in document.getElementsByClassName('test')) {
           console.log("---- " + sortable)
           sortable.setAttribute('class', '');
    }

Th output for this gives error 此输出给出错误

undefined is not a function 未定义不是函数

for sortable.setAttribute('class', ''); 用于sortable.setAttribute('class', ''); line. 线。

And using javascript 2 method 并使用javascript 2方法

   var elements = document.getElementsByClassName("test");

   for (var i=0; i< elements.length;i++) {
         elements[i].setAttribute("class", "");
   }

I get the appropriate result. 我得到适当的结果。

My html code is 我的html代码是

       <span class="test" id="test1">Test1 </span>
        <span class="test" id="test2">Test2 </span>
        <span class="test" id="test3">Test3 </span>

I don't know why I don't get DOM elements in var sortable in document.getElementsByClassName('test') as sortable ? 我不知道为什么不能var sortable in document.getElementsByClassName('test')中将var sortable in document.getElementsByClassName('test') DOM元素var sortable in document.getElementsByClassName('test')sortable

If you do a for .. in loop in JavaScript, it is important to know that the value returned by the for loop is actually the key and not the value. 如果在JavaScript中执行for .. in循环,则重要的是要知道for循环返回的值实际上是键而不是值。

So you could try this: 因此,您可以尝试以下操作:

var testElements = document.getElementsByClassName('test');

for (var key in testElements) {
    var sortable = testElements[key];
    console.log("---- " + sortable)
    sortable.setAttribute('class', '');
}

However, this would fail as there are many properties defined on the value returned by getElementsByClassName , which not all return an element. 但是,这将失败,因为在getElementsByClassName返回的值上定义了许多属性,这些属性并非都返回一个元素。

You can see them all by doing the following: 您可以通过执行以下操作查看所有内容:

for (var key in document.getElementsByClassName('test')) {
    console.log(key);
}

This will output something like this: 这将输出如下内容:

0
1
2
test1
test2
test3
length
item
namedItem

You can see that not only does the returned value contain the numerical indexes, it also has the ID's as properties and also an item and namedItem property. 您可以看到返回的值不仅包含数字索引,还具有ID的属性,以及itemnamedItem属性。

Unfortunatety, your for-loop also doesn't work as you are changing the class name and getElementsByClassName returns a "live" collection, which means that once you change the class, it is removed from the collection. 不幸的是,由于您正在更改类名,因此for循环也不起作用,并且getElementsByClassName返回“活动”集合,这意味着一旦更改了类,便会将其从集合中删除。

You can work around this with a simple loop: 您可以通过一个简单的循环解决此问题:

var elements = document.getElementsByClassName("test");

while (elements.length > 0) {
    elements[0].setAttribute("class", "");
}

This solution was taken from: Javascript For Loop execution on dom element 该解决方案摘自: 在dom元素上执行Javascript For Loop

getElementsByClassName returns an array of all the elements. getElementsByClassName返回所有元素的数组。

For-in only iterates over objects, not arrays. For-in仅迭代对象,而不迭代数组。

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

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