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. Arrays.toString(a)--->将数组a的值转换为字符串

    Arrays.toString(数组)是java内置类Arrays类的一个方法,具体查Api可知.因为数组是不可直接输出的,它的作用是将数组转换为字符串.其实用for循环也是可以做到的,只不过比for ...

  2. C#.NET的微信功能开发学习

    对于新手的我来说,我不是申请一个微信公众号认证后进行开发,我是申请一个订阅号,然后通过自己申请的订阅号的公众平台测试号来学习. 一,申请后成功后,打开开发者工具-公众平台测试号 二,进去公众平台测试号 ...

  3. HDU 2212 DFS

    Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...

  4. 火狐中,设置align="center"失效的解决方法

    如下: <img align="center" style="vertical-align:top"></img> 只需要加上: ver ...

  5. autocomplete+PHP+MYSQL的实现模糊查询

    1.HTML网页表单部分: <input type="text" name="course" id="course" /> 2. ...

  6. 原生态 php连接mysql

    <?php$host = 'localhost';$user = 'root';$pass = '';$dbname = 'test';$con = mysql_connect($host,$u ...

  7. IdentityDbContext类源码

    Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs 源码: 其中涉及到用户信息表.用户角色表的相关操作. using Syst ...

  8. Union、Union All、Except、InterSect的区别

    UNION: 将多个「结果集 (result set)」的「行 (row)」合并,作为单个结果集返回,并移除重复的行.若有重复的行,只留下一个. UNION ALL: 将多个「结果集 (result ...

  9. Qt4.8.x Linux WebKit依赖库安装

    yum install "pkgconfig(gstreamer-app-0.10)"

  10. gcc-config: Active gcc profile is invalid解决办法

    错误描述 Gentoo软件安装错误,提示: gcc-config: Active gcc profile is invalid 解决方法: 列出可用的profile gcc-config -l gcc ...