23 Star 49 Fork 9

KoalaLC/pptHtml

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
classList.js 3.38 KB
一键复制 编辑 原始数据 按行查看 历史
KoalaLC 提交于 2015-04-17 00:12 . first commit
/*
* classList.js: Implements a cross-browser element.classList getter.
* 2010-09-06
*
* By Eli Grey, http://eligrey.com
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
"use strict";
if (typeof Element !== "undefined") {
(function () {
var
classListProp = "classList"
, protoProp = "prototype"
, elemCtrProto = Element[protoProp]
, objCtr = Object
;
if (!objCtr.hasOwnProperty.call(elemCtrProto, classListProp)) {
var
strTrim = String[protoProp].trim || function () {
return this.replace(/^\s+|\s+$/g, "");
}
, arrIndexOf = Array[protoProp].indexOf || function (item) {
for (var i = 0, len = this.length; i < len; i++) {
if (i in this && this[i] === item) {
return i;
}
}
return -1;
}
, checkTokenAndGetIndex = function (classList, token) {
if (token === "") {
throw "SYNTAX_ERR";
}
if (/\s/.test(token)) {
throw "INVALID_CHARACTER_ERR";
}
return arrIndexOf.call(classList, token);
}
, ClassList = function (elem) {
var
trimmedClasses = strTrim.call(elem.className)
, classes = trimmedClasses ? trimmedClasses.split(/\s+/) : []
;
for (var i = 0, len = classes.length; i < len; i++) {
this.push(classes[i]);
}
this.updateClassName = function () {
elem.className = this.toString();
};
}
, classListProto = ClassList[protoProp] = []
, classListGetter = function () {
return new ClassList(this);
}
;
classListProto.item = function (i) {
return this[i] || null;
};
classListProto.contains = function (token) {
token += "";
return checkTokenAndGetIndex(this, token) !== -1;
};
classListProto.add = function (token) {
token += "";
if (checkTokenAndGetIndex(this, token) === -1) {
this.push(token);
this.updateClassName();
}
};
classListProto.remove = function (token) {
token += "";
var index = checkTokenAndGetIndex(this, token);
if (index !== -1) {
this.splice(index, 1);
this.updateClassName();
}
};
classListProto.toggle = function (token) {
token += "";
if (checkTokenAndGetIndex(this, token) === -1) {
this.add(token);
} else {
this.remove(token);
}
};
classListProto.toString = function () {
return this.join(" ");
};
if (objCtr.defineProperty) {
var classListDescriptor = {
get: classListGetter
, enumerable: true
, configurable: true
};
try {
objCtr.defineProperty(elemCtrProto, classListProp, classListDescriptor);
} catch (ex) { // IE 8 doesn't support enumerable:true
if (ex.number === -0x7FF5EC54) {
classListDescriptor.enumerable = false;
objCtr.defineProperty(elemCtrProto, classListProp, classListDescriptor);
}
}
} else if (objCtr[protoProp].__defineGetter__) {
elemCtrProto.__defineGetter__(classListProp, classListGetter);
}
}
}());
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/koalalc/pptHtml.git
git@gitee.com:koalalc/pptHtml.git
koalalc
pptHtml
pptHtml
master

搜索帮助