最近的Chrome更改为getElementsByClassName

时间:2014-07-12 06:28:47

标签: javascript arrays google-chrome getelementsbyclassname

因此,在编写IIFE API时,我意识到chrome已经更改了getElementsByClassName DOM属性。出于某种原因,他们之前很难获得我们想要的物品。例如

 [item: function, namedItem: function]
   0: div.sceditor-group
   1: div.sceditor-group
   2: div.sceditor-group
   3: div.sceditor-group
   4: div.sceditor-group
   5: div.sceditor-group
   6: div.sceditor-group
   7: div.sceditor-group
   8: div.sceditor-group
   9: div.sceditor-group
   length: 10
   __proto__: HTMLCollection

这是控制台中记录的内容。虽然我不能再这样做了

 var a = document.getElementsByClassName('sceditor-group');
 console.log(a.length);
 //OR THIS
 for(i=0;i<a.length;i++){
    //manipulate each element
    //since i now returns 0 for some reason
 }

但是,如果我们查看返回的数组/对象或现在可能是什么。它有一个长度。那么我们现在如何得到长度属性!!

IIFE实际位

 var t = getElement('class','sceditor-group');
    console.log(t);
    console.log(t.length);
    this.groups = {};
    for(i=0;i<t.length;i++){
      this.groups[i] = t[i];
    }
    this.groups.length = t.length;

getElement功能

function getElement(t, ele) {
        switch (t) {
        case "id":
            return document.getElementById(ele);
        case "class":
            return document.getElementsByClassName(ele);
        case "element":
            return document.getElementsByTagName(ele);
        case "input":
            return document.getElementsByName(ele);
        case "query":
            return document.querySelectorAll(ele);
        }
    }

**回复**

[item: function, namedItem: function]
  0: div.sceditor-group
  1: div.sceditor-group
  2: div.sceditor-group
  3: div.sceditor-group
  4: div.sceditor-group
  5: div.sceditor-group
  length: 6
  __proto__: HTMLCollection

  0 

  Object {length: 0} 

0 个答案:

没有答案