Skip to content

Commit

Permalink
新增操作Class的两个函数
Browse files Browse the repository at this point in the history
  • Loading branch information
small-Black committed Mar 31, 2016
1 parent e6a9dbf commit 39f10a8
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Tool/js/Tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,46 @@
}
return argUrl;
}
// 添加Class
function addClass(elem, value) {
var classNames, i, l,
setClass, c, cl,
space = /\s+/;
if (value && typeof value === "string") {
classNames = value.split(space);
if (elem.nodeType === 1) {
if (!elem.className && classNames.length === 1) {
elem.className = value;

} else {
setClass = " " + elem.className + " ";

for (c = 0, cl = classNames.length; c < cl; c++) {
if (setClass.indexOf(" " + classNames[c] + " ") < 0) {
setClass += classNames[c] + " ";
}
}
elem.className = setClass.replace(/(^\s*)|(\s*$)/g, "");
}
}
}
}
// 移除Class
function removeClass(elem, value) {
var removes, className, c, cl, i, l, space = /\s+/,
rclass = /[\t\r\n]/g;
if ((value && typeof value === "string") || value === undefined) {
removes = (value || "").split(space);
if (elem.nodeType === 1 && elem.className) {

className = (" " + elem.className + " ").replace(rclass, " ");
for (c = 0, cl = removes.length; c < cl; c++) {
while (className.indexOf(" " + removes[c] + " ") >= 0) {
className = className.replace(" " + removes[c] + " ", " ");
}
}
elem.className = value ? className.replace(/(^\s*)|(\s*$)/g, "") : "";
}
}

}

0 comments on commit 39f10a8

Please sign in to comment.