document.querySelectorAll兼容性良好,在之前的项目中就其遍历方式出了错误,先做个小结:

1.for循环 传统遍历方法

for(var i= 0; i< document.querySelectopAll(".a").length; i ++){

document.querySelectopAll(".a")[i].style.color= "red";

}

2.forEach方法

forEach方法可以遍历一个js数组

var arr= [1, 2, 3];

arr.forEach(arr, function(pre){})

兼容性: 均兼容,IE低版本不兼容,本人使用的是IE9

若不兼容,可手动扩展:

详情:http://blog.csdn.net/oscar999/article/details/8671546

if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError(" this is null or not defined");
}
var O = Object(this);
var len = O.length >>> ; // Hack to convert O.length to a UInt32
if ({}.toString.call(callback) != "[object Function]") {
throw new TypeError(callback + " is not a function");
}
if (thisArg) {
T = thisArg;
}
k = ;
while (k < len) {
var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k++;
}
};
}

如果这样使用:

document.querySelectorAll(".aa").forEach(function(){
    console.log("1")    
})

会报错,应为document.querySelectorAll(".aa")是一个NodeList数组,不是一般js数组!

可以借助call来实现

[].forEach.call(document.querySelectorAll(".aa"), function(){ 

  console.log("1")       

});

document.querySelectorAll遍历(forEach小解)的更多相关文章

  1. document.querySelectorAll遍历

    document.querySelectorAll兼容性良好,在之前的项目中就其遍历方式出了错误,先做个小结: 1.for循环 传统遍历方法 for(var i= 0; i< document. ...

  2. 如何循环遍历document.querySelectorAll()方法返回的结果

    使用JavaScript的forEach方法,我们可以轻松的循环一个数组,但如果你认为document.querySelectorAll()方法返回的应该是个数组,而使用forEach循环它: /* ...

  3. HTML5中类jQuery选择器querySelector的高级使用 document.querySelectorAll.bind(document);

    基本用法 querySelector 该方法返回满足条件的单个元素.按照深度优先和先序遍历的原则使用参数提供的CSS选择器在DOM进行查找,返回第一个满足条件的元素. ----> querySe ...

  4. Array.prototype.slice.call(document.querySelectorAll('a'), 0)

    Array.prototype.slice.call(document.querySelectorAll('a'), 0)的作用就是将一个DOM NodeList 转换成一个数组. slice()方法 ...

  5. document.querySelector()和document.querySelectorAll()

    HTML5向Web API新引入了 document.querySelector()和document.querySelectorAll()两个方法,都可以接收三种类型的参数:id(#),class( ...

  6. document.querySelectorAll() 与document.getElementTagName() 的区别

    这个区别我估计大神都不知道,问题源于博主,细节被一个妹子发现的 事情经过是这样 <ul> <li>item</li> <li></li> & ...

  7. document.querySelectorAll() 兼容 IE6

    不多说,直接上代码 // 使用 css 选择器获取元素对象 兼容性封装 Test Already. function getElementsByCss(cssStr){ if(document.que ...

  8. [19/03/12-星期二] 数组_遍历(for-each)&复制&java.util.Arrays类

    一.遍历 for-each即增强for循环,是JDK1.5新增加的功能,专门用于读取数组或集合中所有的元素,即对数组进行遍历. //数组遍历 for-each public class Test_03 ...

  9. JS - 把类似document.querySelectorAll(".xxx")、document.getElementsByName("xxx")这种方法的返回结果转换成数组对象

    var btns = document.querySelectorAll(".btn");console.log(btns instanceof Array); // falseb ...

随机推荐

  1. 彻底解决tap“点透”,提升移动端点击响应速度

    申明!!!最后发现判断有误,各位读读就好,正在研究中.....尼玛水太深了 前言 近期使用tap事件为老夫带来了这样那样的问题,其中一个问题是解决了点透还需要将原来一个个click变为tap,这样的话 ...

  2. Apache httpd.conf配置详解

    常用配置指令说明 1. ServerRoot:服务器的基础目录,一般来说它将包含conf/和logs/子目录,其它配置文件的相对路径即基于此目录.默认为安装目录,不需更改. 语法:ServerRoot ...

  3. Qt_DX

    #ifndef MY_FRAME__HH__ #define MY_FRAME__HH__ #include <QtGui/QWidget> struct IDirect3D9; stru ...

  4. hadoop---wordcount命令

    [zznu@master file]$ hadoop jar ~/hadoop-2.5.2/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.2 ...

  5. 用 rebar 来构建、编译、测试、发布 Erlang 应用程序

    转自:http://dhq.me/build-compile-eunit-release-erlang-application-with-rebar rebar 是一个遵循 Erlang/OTP 原则 ...

  6. Spring测试

    测试类添加两个注解 @RunWith(SpringJUnit4ClassRunner.class)和@ContextConfiguration(locations = "classpath: ...

  7. Entity Framework Tools install to VS 2015

    因为在VS2013,2015里不再支持Sql compact 数据库的显示, 但是我们可以通过安装EF tools扩展来支持,参考地址:http://thedatafarm.com/data-acce ...

  8. CVE-2014-4113 Win8.1 64位利用(2014.11)

    CVE-2014-4113 Win8.1 64位利用 关于漏洞成因的分析已经很多了,但是样本针对的是win8之前的利用,国外研究员Moritz Jodeit对Win8.1上的利用做了研究,并给出了思路 ...

  9. js预编译

    先来做三个测试 eg1: var a; a = 1; function a() {}; console.log(a); eg2: var a; function a() {}; console.log ...

  10. Power oj2498/DP/递推

    power oj 2498 /递推 2498: 新年礼物 Time Limit: 1000 MS Memory Limit: 65536 KBTotal Submit: 12 Accepted: 3  ...