document.querySelectorAll遍历
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 >>> 0; // 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 = 0;
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遍历的更多相关文章
- document.querySelectorAll遍历(forEach小解)
document.querySelectorAll兼容性良好,在之前的项目中就其遍历方式出了错误,先做个小结: 1.for循环 传统遍历方法 for(var i= 0; i< document. ...
- 如何循环遍历document.querySelectorAll()方法返回的结果
使用JavaScript的forEach方法,我们可以轻松的循环一个数组,但如果你认为document.querySelectorAll()方法返回的应该是个数组,而使用forEach循环它: /* ...
- HTML5中类jQuery选择器querySelector的高级使用 document.querySelectorAll.bind(document);
基本用法 querySelector 该方法返回满足条件的单个元素.按照深度优先和先序遍历的原则使用参数提供的CSS选择器在DOM进行查找,返回第一个满足条件的元素. ----> querySe ...
- document.querySelector()和document.querySelectorAll()
HTML5向Web API新引入了 document.querySelector()和document.querySelectorAll()两个方法,都可以接收三种类型的参数:id(#),class( ...
- Array.prototype.slice.call(document.querySelectorAll('a'), 0)
Array.prototype.slice.call(document.querySelectorAll('a'), 0)的作用就是将一个DOM NodeList 转换成一个数组. slice()方法 ...
- document.querySelectorAll() 与document.getElementTagName() 的区别
这个区别我估计大神都不知道,问题源于博主,细节被一个妹子发现的 事情经过是这样 <ul> <li>item</li> <li></li> & ...
- document.querySelectorAll() 兼容 IE6
不多说,直接上代码 // 使用 css 选择器获取元素对象 兼容性封装 Test Already. function getElementsByCss(cssStr){ if(document.que ...
- JS - 把类似document.querySelectorAll(".xxx")、document.getElementsByName("xxx")这种方法的返回结果转换成数组对象
var btns = document.querySelectorAll(".btn");console.log(btns instanceof Array); // falseb ...
- document.querySelector()与document.querySelectorAll()
document.querySelector()与document.querySelectorAll() CreationTime--2018年7月1日10点49分 Author:Marydon ...
随机推荐
- apache thinkphp5 强制https://访问
根目录下,.htaccess文件 <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On ...
- inter® management engine interface黄色感叹号解决方法
win10今天安装电脑驱动时发现inter® management engine interface怎么装都是黄色感叹号,所以做了下以下得测试 1.inter® management engine ...
- iTerm2快速SSH连接并保存密码
背景 Mac自带terminal,以及比较好用的iTerm2命令行工具,都缺乏一个功能,就是远程SSH连接,无法保存密码.一种方法是将本机的ssh_key放到远程服务器中实现无密码登录.这种方法在很多 ...
- meta 中的属性viewport
粘贴自:https://blog.csdn.net/u012402190/article/details/70172371 <meta name="viewport" con ...
- 关于Linux连接工具mobaxterm显示中文乱码问题
本人用的是MobaXterm Personal 9.1版本.近期发现连接上服务器,查看日志时,发现中文乱码,无法正常显示.甚是苦恼.百度搜索该工具显示乱码问题,无一人解决.提倡更换连接工具.无意间发现 ...
- TCP协议之网络延时
影响TCP 网络时延的因素硬件速度网络和服务器的负载请求和响应报文的尺寸客户端和服务器之间的距离TCP 协议的技术复杂性TCP协议产生的时延TCP 连接建立握手:TCP 慢启动拥塞控制:数据聚集的 N ...
- HashMap源码分析二
jdk1.2中HashMap的源码和jdk1.3中HashMap的源码基本上没变.在上篇中,我纠结的那个11和101的问题,在这边中找到答案了. jdk1.2 public HashMap() ...
- 【转载】SELENIUM2支持无界面操作(HTMLUNIT和PHANTOMJS)
SELENIUM2支持无界面操作(HTMLUNIT和PHANTOMJS) selenium2支持通过各种driver(FirfoxDriver,IternetExplorerDriver,OperaD ...
- centos6、7系统初始化脚本
#!/bin/bash # #******************************************************************** #encoding -*-utf ...
- 【bfs分层图 dp】hihocoder#1147 : 时空阵
最短路径树上分层dp的一类套路吧 题目大意 幽香这几天学习了魔法,准备建造一个大型的时空传送阵. 幽香现在可以在幻想乡的n个地点建造一些传送门,如果她建造了从地点a与地点b之间的传送门,那么从a到b和 ...