利用Array Prototype的方法来实现对dom集合的筛选、indexOf、map等功能
<!DOCTYPE html><html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
div{background-color:red;margin:10px;}
</style>
</head>
<body>
<div class="w50" style="width:50px;height:20px"></div>
<div class="w20" style="width:20px;height:20px"></div>
<div class="w30" id="test" style="width:30px;height:20px"></div>
<div class="w40" style="width:40px;height:20px"></div>
<div class="w10" style="width:10px;height:20px"></div>
</body>
</html>var arrPro = Array.prototype;
var core_filter = arrPro.filter;
var core_indexOf = arrPro.indexOf;
var core_slide = arrPro.slice;
var core_sort = arrPro.sort;
var core_map = arrPro.map; //当然 为了兼容,应该有toArray方法,对不支持slice.call(nodeList)的浏览器,使用循环
var divArr = core_slide.call(document.getElementsByTagName('div')); console.log('筛选出所有宽度小于等于30px的div',core_filter.call(divArr,function(item,index){
return parseInt(item.style.width,10) <= 30;
}));
console.log('获取#test元素的索引',core_indexOf.call(divArr,document.getElementById('test')));
console.log('按照宽度倒叙排序',core_sort.call(divArr,function(a,b){
return parseInt(a.style.width,10) < parseInt(b.style.width,10);
}));
//将所有div背景色改为黑色
core_map.call(divArr,function(item){
item.style.backgroundColor = 'black';
});firebug截图:

利用Array Prototype的方法来实现对dom集合的筛选、indexOf、map等功能的更多相关文章
- 利用Object.prototype.toString方法,实现比typeof更准确的type校验
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同 ...
- 利用过滤器Filter和特性Attribute实现对Web API返回结果的封装和统一异常处理
在我们开发Web API应用的时候,我们可以借鉴ABP框架的过滤器Filter和特性Attribute的应用,实现对Web API返回结果的封装和统一异常处理,本篇随笔介绍利用AuthorizeAtt ...
- 数组的方法之(Array.prototype.forEach() 方法)
forEach() 方法对数组的每个元素执行一次提供的函数. 注意: 没有返回一个新数组 并且 没有返回值! 应用场景:为一些相同的元素,绑定事件处理器! const arr = ['a', 'b', ...
- 数组Array和字符串String的indexOf方法,以及ES7(ES2016)中新增的Array.prototype.includes方法
前言 我们在判断某一个字符是否存在于一个字符串中或者某一个值是否存在于一个数组中时,ES7之前我们需要使用indexOf,ES7引入了新的方法includes 语法 数组:Array.inexOf(s ...
- 数组的方法之(Array.prototype.reduce() 方法)
reduce函数 reduce() 方法对累加器和数组中的每个元素(从左到右)应用一个函数,将其减少为单个值. 对数组中的所有元素调用指定的回调函数.该回调函数的返回值为累积结果,并且此返回值在下一次 ...
- 数组的方法之(Array.prototype.filter() 方法)
filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素. 注意: filter() 不会对空数组进行检测. 注意: filter() 不会改变原始 ...
- 利用 Java 操作 Jenkins API 实现对 Jenkins 的控制详解
本文转载自利用 Java 操作 Jenkins API 实现对 Jenkins 的控制详解 导语 由于最近工作需要利用 Jenkins 远程 API 操作 Jenkins 来完成一些列操作,就抽空研究 ...
- Array.prototype.sort()对数组对象排序的方法
Array.prototype.sort()方法接受一个参数——Function,Function会提供两个参数,分别是两个进行比较的元素,如果元素是String类型则通过Unicode code进行 ...
- JavaScript中的Array.prototype.slice.call()方法学习
JavaScript中的Array.prototype.slice.call(arguments)能将有length属性的对象转换为数组(特别注意: 这个对象一定要有length属性). 但有一个例外 ...
随机推荐
- k8s环境搭建--基于minik8s方法
minik8s 安装 关闭selinux.开启ipv6 sudo bash selinux_ipv6.sh 下载kubectl和minikube 下载minikube,因为国外的源被墙了,所以只能用阿 ...
- 构造一个简单的Linux内核的MenuOS
构造一个简单的Linux内核的MenuOS 20135109 高艺桐 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000 ...
- 奔跑吧DKY——团队Scrum冲刺阶段博客汇总
第一周:团队展示 团队选题 需求规格说明书 第二周:完善需求规格说明书.制定团队编码规范.通过团队项目数据库设计 奔跑吧DKY--团队Scrum冲刺阶段-Day 1-领航 奔跑吧DKY--团队Scru ...
- 如何获取启动页activity
启动页activity指App启动的第一个activity,介绍几种查看启动页activity的方法: 方法一:问开发,最有效的获取方式 方法二:dumpsys package 包名,前提是知道包名( ...
- java语言复制数组的四种方法
JAVA语言的下面几种数组复制方法中,哪个效率最高? B.效率:System.arraycopy > clone > Arrays.copyOf > for循环 1.System.a ...
- 《大象Think in UML》阅读笔记之一
Think in UML这一书以UML为载体,将面向对象的分析设计思想巧妙地融合在建模UML当中,通过一些实例将软件系统的开发过程中的一些知识有机地结合起来.全书共分为四篇:准备篇.基础篇.进阶篇和总 ...
- springmvc 映射重复
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springfr ...
- 【贪心算法】POJ-2376 区间问题
一.题目 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cle ...
- mvc 页面方法学习-RenderBody方法
教程地址:https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/getting-started/introduction/adding-a-view
- js汉字按字母排序
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...