classlist和array.prototype.slice.call
1.classlist
document.getElementById("myDIV").classList.add("mystyle");
classList 属性返回元素的类名,作为 DOMTokenList 对象。
该属性用于在元素中添加,移除及切换 CSS 类。
classList 属性是只读的,但你可以使用 add() 和 remove() 方法修改它。
返回值:一个 DOMTokenList, 包含元素的类名列表
| 方法 | 描述 |
|---|---|
| add(class1, class2, ...) | 在元素中添加一个或多个类名。
如果指定的类名已存在,则不会添加 |
| contains(class) | 返回布尔值,判断指定的类名是否存在。
可能值:
|
| item(index) | 返回类名在元素中的索引值。索引值从 0 开始。
如果索引值在区间范围外则返回 null |
| remove(class1, class2, ...) | 移除元素中一个或多个类名。
注意: 移除不存在的类名,不会报错。 |
| toggle(class, true|false) | 在元素中切换类名。
第一个参数为要在元素中移除的类名,并返回 false。 第二个是可选参数,是个布尔值用于设置元素是否强制添加或移除类,不管该类名是否存在。例如: 移除一个 class: element.classList.toggle("classToRemove", false); 注意: Internet Explorer 或 Opera 12 及其更早版本不支持第二个参数。 |
2.array.prototype.slice.call
在很多时候经常看到Array.prototype.slice.call()方法,比如Array.prototype.slice.call(arguments),下面总结一下其原理:
2.1 在JS里Array是一个类 slice是此类里的一个方法 ,那么使用此方法应该Array.prototype.slice这么去用
slice从字面上的意思很容易理解就是截取
arrayObj.slice(start, [end])
2.2 call
call([thisObj[,arg1[arg2[[argN]]]]])
thisObj是一个对象的方法
arg1~argN是参数
Array.prototype.slice.call(arguments,1);
这句话的意思就是说把调用方法的参数截取出来。
如:
function test(a,b,c,d) {
var arg = Array.prototype.slice.call(arguments,1);
alert(arg);
}
test("a","b","c","d"); //b,c,d
Array.prototype.slice.call(arguments, 1),不就是等于 arguments.slice(1) 吗?答案是否定的
因为arguments并不是真正的数组对象,只是与数组类似而已,所以它并没有slice这个方法(slice本来只是Array和 String的方法),而
Array.prototype.slice.call(arguments, 1)
可以理解成是让arguments转换成一个数组对象,让arguments具有slice()方法。要是直接写arguments.slice(1)会报错。
var a={length:2,0:'first',1:'second'};//类数组,有length属性,长度为2,第0个是first,第1个是second
console.log(Array.prototype.slice.call(a,0));// ["first", "second"],调用数组的slice(0);
var a={length:2,0:'first',1:'second'};console.log(Array.prototype.slice.call(a,1));//["second"],调用数组的slice(1);
var a={0:'first',1:'second'};//去掉length属性,返回一个空数组console.log(Array.prototype.slice.call(a,0));//[]
function test(){ console.log(Array.prototype.slice.call(arguments,0));//["a", "b", "c"],slice(0)
console.log(Array.prototype.slice.call(arguments,1));//["b", "c"],slice(1)}test("a","b","c");
核心:Array.prototype.slice.call(arguments)可以将 类数组 转化为真正的数组。
补充:
什么是类数组(有length属性,属性值为数字;其他属性值为数字‘0’,‘1’,等)
var myobject ={ // array-like collection
length: 4,
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three'
}
3.将函数的实际参数转为数组的方法
方法一
var args = Array.prototype.slice.call(arguments);
方法二
var args = [].slice.call(arguments, 0);
方法三
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
最后,附个转成数组的通用函数
var toArray = function(s){
try{return Array.prototype.slice.call(s);
}
catch(e){
var arr = [];
for(var i = 0,len = s.length; i < len; i++){//arr.push(s[i]);
arr[i] = s[i]; //据说这样比push快}
return arr;
}
}
参考:http://www.th7.cn/web/js/201511/133059.shtml
classlist和array.prototype.slice.call的更多相关文章
- 【javascript 技巧】Array.prototype.slice的妙用
Array.prototype.slice的妙用 开门见山,关于Array 的slice的用法可以参考这里 http://www.w3school.com.cn/js/jsref_slice_arra ...
- Array.prototype.slice.call(arguments)
Array.prototype.slice.call(arguments)能够将具有length属性的对象转化为数组, 可以理解为将arguments转化成一个数组对象,让它具有slice方法 如: ...
- IE下Array.prototype.slice.call(params,0)
i8 不支持 Array.prototype.slice.call(params,0) params可以是 HTMLCollection.类数组.string字符串
- (转)Array.prototype.slice.call自解
很多框架或者库里面都会有这句的使用,最多的还是通过Array.prototype.slice.call(arguments,0)把arguments这个伪数组转换为真正的数组.但为什么可以这么做,却一 ...
- 详解 Array.prototype.slice.call(arguments)
首先,slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组 在这里我们看第二个方法 1.在JS里Array是一个类 slice是 ...
- Array.prototype.slice && Array.prototype.splice 用法阐述
目的 对于这两个数组操作接口,由于不理解, 往往被误用, 或者不知道如何使用.本文尝试给出容易理解的阐述. 数组 什么是数组? 数组是一个基本的数据结构, 是一个在内存中依照线性方式组织元素的方式, ...
- Array.prototype.slice.call(document.querySelectorAll('a'), 0)
Array.prototype.slice.call(document.querySelectorAll('a'), 0)的作用就是将一个DOM NodeList 转换成一个数组. slice()方法 ...
- Array.prototype.slice.call
Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组 ,::'age'}; Array.prototype.slice.call(arr); ...
- Array.prototype.slice.call(arguments) 类数组转成真正的数组
Array.prototype.slice.call(arguments) 我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...
随机推荐
- n后问题
Description 在n×n格的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于在n×n格的棋盘上放置n个皇后,任何2个皇 ...
- 三、hive JavaAPI示例
在上文中https://www.cnblogs.com/lay2017/p/9973370.html 我们通过hive shell去操作hive,本文我们以Java代码的示例去对hive执行加载数据和 ...
- JDBC处理事务
一.什么是事务? 在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成一个事务! 二.事务是必须满足4个条件(AC ...
- sql:SQL Server metadata queries
http://www.mssqltips.com/sqlservertip/3449/making-sql-server-metadata-queries-easier-with-these-new- ...
- 洛谷P2792 [JSOI2008]小店购物(最小树形图)
题意 题目链接 Sol 一开始的思路:新建一个虚点向每个点连边,再加上题面中给出的边,边权均为大小*需要购买的数量 然后发现死活都过不去 看了题解才发现题目中有个细节--买了\(A\)就可以买\(B\ ...
- Http Status Code 套餐合集(转载)
1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 100(继续)请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101(切换协议)请求者已要 ...
- 收藏的几个关于php面向对象教程
面向对象的分析方法是利用面向对象的信息建模概念,如实体.关系.属性等,同时运用封装.继承.多态等机制来构造模拟现实系统的方法,学会了面向对象思想,能够大大提高php编程开发效率!本篇文章php中文网将 ...
- 【js常用DOM方法】
介绍几个js DOM的常用方法 获取元素节点 getElementById getElementsByTagName getElementsByClassName 先写一个简单的网页做测试: /* ...
- 给大家分享下坐标转换的代码的JS和Python两个版本的源码【转】
/** * Created by Wandergis on 2015/7/8. * 提供了百度坐标(BD09).国测局坐标(火星坐标,GCJ02).和WGS84坐标系之间的转换 */ /** * 百度 ...
- Selenium对浏览器的支持
1.火狐浏览器 优点:FireFox Dirver对页面的自动化测试支持得比较好,很直观地模拟页面的操作,对JavaScript的支持也非常完善,基本上页面上做的所有操作FireFox Driver都 ...