Tool库
var Tool = function() {
this.flag = "getElementsByClassName" in document;
};
Tool.prototype = {
constructor: Tool,
getElementsByClassName: function(context, cName) {
var context = context || document;
var ary = [];
if (this.flag) {
return this.listToArray(context.getElementsByClassName(cName));
}
var allNode = context.getElementsByTagName("*");
var reg = new RegExp("(?:^| +)" + cName + "(?: +|$)");
for (var i = 0; i < allNode.length; i++) {
var cur = allNode[i];
if (reg.test(cur.className)) {
ary.push(cur);
}
}
return ary;
},
toJSON: function(jsonStr) {
var jsonObj = null;
try {
jsonObj = JSON.parse(jsonStr);
} catch (e) {
jsonObj = eval("(" + jsonStr + ")");
}
return jsonObj;
},
isType: function(value, type) {
var type = arguments[1] || "object",
reg = new RegExp("\\[object " + type + "\\]", "i");
return reg.test(Object.prototype.toString.call(value));
},
listToArray: function(likeAry) {
var ary = [];
if (this.flag) {
ary = Array.prototype.slice.call(likeAry, 0);
} else {
for (var i = 0; i < likeAry.length; i++) {
ary.push(likeAry[i]);
}
}
return ary;
},
getEleChildren: function(parent, tagName) {
var allNode = parent.childNodes,
ary = [],
reg = new RegExp("^" + tagName + "&", "i");
for (var i = 0; i < allNode.length; i++) {
var cur = allNode[i];
if (cur.nodeType === 1) {
if (tagName) {
if (reg.test(cur.nodeName)) {
ary.push(cur);
}
continue;
}
ary.push(cur);
}
}
return ary;
},
getFirst: function(curEle) {
var childern = this.getEleChildren(curEle);
return childern.length > 0 ? childern[0] : null;
},
getLast: function(curEle) {
var childern = this.getEleChildren(curEle);
return childern.length > 0 ? childern[childern.length - 1] : null;
},
getPre: function(curEle) {
if (this.flag) {
return curEle.previousElementSibling;
}
var pre = curEle.previousSibling;
while (pre && pre.nodeType !== 1) {
pre = pre.previousSibling;
}
return pre;
},
getPres: function(curEle) {
var ary = [],
next = this.getPre(curEle);
while (next) {
ary.unshift(next);
next = this.getPre(next);
}
return ary;
},
getNext: function(curEle) {
if (this.flag) {
return curEle.nextElementSibling;
}
var next = curEle.nextSibling;
while (next && next.nodeType !== 1) {
next = next.nextSibling;
}
return next;
},
getNexts: function(curEle) {
var ary = [],
next = this.getNext(curEle);
while (next) {
ary.unshift(next);
next = this.getNext(next);
}
return ary;
},
getSibling: function(curEle) {
var ary = [],
pre = this.getPre(curEle),
next = this.getNext(curEle);
pre ? ary.push(pre) : void 0;
next ? ary.push(next) : void 0;
return ary;
},
getSiblings: function(curEle) {
var pres = this.getPres(curEle),
nexts = this.getNexts(curEle);
return pres.concat(nexts);
},
getIndex: function(curEle) {
return this.getPres(curEle).length;
},
insertAfter: function(newEle, oldEle) {
var next = this.getNext(oldEle),
par = oldEle.parentNode;
next ? par.insertBefore(newEle, next) : par.appendChild(newEle);
},
prependChild: function(parentEle, curEle) {
var first = this.getFirst(parentEle);
first ? parentEle.insertBefore(curEle, first) : parentEle.appendChild(curEle);
},
innerHTML: function(curEle, str) {
var str = str || "";
if (!str) {
return curEle.innerHTML;
}
curEle.innerHTML = str;
},
text: function(ele, str) {
if (ele && ele.nodeType && ele.nodeType == 1) {
if (str === undefined) {
if (typeof ele.textContent == 'string')
return ele.textContent;
else
return ele.innerText;
} else {
if (str === null) {
alert('text方法参数错误,str为null!');
return;
} else if (typeof str == 'string') {
if (typeof ele.textContent == 'string') {
ele.textContent += str;
} else {
ele.innerText += str;
}
} else {
alert('text方法的参数错误!')
}
}
} else {
alert('text方法的ele参数错误!')
}
},
setCss: function(curEle, attr, value) {
if (typeof value === "undefined") {
var reg = /^(?:margin|padding|border|float|position|display|background|backgroundColor)$/;
var value = this.flag ? window.getComputedStyle(curEle, null)[attr] : curEle.currentStyle[attr];
return !reg.test(attr) ? parseFloat(value) : value;
} else {
switch (attr) {
case "opacity":
curEle["style"][attr] = value;
curEle["style"]["filter"] = "alpha(opacity=" + (value * 100) + ")";
break;
case "float":
curEle["style"].cssFloat = value;
curEle["style"].styleFloat = value;
break;
case "backgroundColor":
curEle["style"][attr] = value;
break;
case "zIndex":
curEle["style"][attr] = value;
break;
default:
curEle["style"][attr] = !isNaN(value) ? value += "px" : value;
}
}
},
setGroupCss: function(curEle, cssObj) {
for (var key in cssObj) {
this.setCss(curEle, key, cssObj[key]);
}
},
offset: function(curEle) {
var par = curEle.offsetParent,
left = curEle.offsetLeft,
top = curEle.offsetTop;
while (par) {
left += par.offsetLeft;
top += par.offsetTop;
if (navigator.userAgent.indexOf("MSIE 8.0") <= -1) {
left += par.clientLeft;
top += par.clientTop;
}
par = par.offsetParent;
}
return {
left: left, top: top };
},
hasClassName: function(curEle, cName) {
var reg = new RegExp("(?:^| +)" + cName + "(?: +|$)");
return reg.test(curEle.className);
},
addClassName: function(curEle, cName) {
if (!this.hasClassName(curEle, cName)) {
curEle.className += (" " + cName);
}
},
removeClassName: function(curEle, cName) {
var reg = new RegExp("(?:^| +)" + cName + "(?: +|$)", "g");
if (this.hasClassName(curEle, cName)) {
curEle.className = curEle.className.replace(reg, " ");
}
}
};
~ function() {
Array.prototype.removeRepeat = function() {
let obj = {
};
for (let i = 0; i < this.length; i++) {
if (obj[this[i]] === undefined) {
obj[this[i]] = this[i];
} else {
this[i--] = this[this.length - 1];
this.length--;
}
}
obj = null;
return this;
};
Array.prototype.forEachPro = function(fn, context) {
var context = context || window;
if (this.forEach) {
this.forEach(fn, context);
return this;
}
for (var i = 0; i < this.length; i++) {
fn.call(context, this[i], i, this);
}
return this;
};
Object.prototype.myHasProperty = function(attr) {
return (attr in this) && !this.hasOwnProperty(attr);
};
String.prototype.myTrim = function() {
return this.replace(/^\s*|\s*$/g, "");
};
String.prototype.mySub = function(len, isD) {
var len = len || 10,
isD = isD || false,
str = "",
n = 0;
for (var i = 0; i < this.length; i++) {
var s = this.charAt(i);
/[\u4e00-\u9fa5]/.test(s) ? n += 2 : n++;
if (n > len) {
isD ? str += "..." : void 0;
break;
}
str += s;
}
return str;
};
String.prototype.myFormatTime = function(templete) {
let timeAry = this.match(/\d+/g);
var templete = templete || "{0}年{1}月{2}日 {3}时{4}分{5}秒";
templete = templete.replace(/\{(\d+)\}/g, (content, $1) => {
let time = timeAry[$1] || "00";
time.length < 2 ? time = "0" + time : time;
return time;
});
return templete;
};
String.prototype.myQueryURLParams = function() {
let obj = {
};
this.replace(/([^?=]+)=([^?=]+)/g, (...[, $1, $2]) => {
obj[$1] = obj[$2] });
this.replace(/#([^?=]+)/g, (...[, $1]) => obj['HASH'] = $1);
return obj;
};
String.prototype.millimeter = function() {
return this.replace(/\d{1,3}(?=(\d{3})+$)/g, content => content + ',');
};
}();