var reg = { "html" : /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/ //匹配html代码 }; var allFinded = []; /** * @author wsf * 检测浏览器版本 */ var browser = { "explorer": { "ie": /msie ([\d.]+).*\.net clr (\d\.){1,2}\d+\)$/, "firefox": /firefox\/([\d.]+)/, "chrome": /chrome\/([\d.]+)/, "opera": /opera.([\d.]+)/, "safari": /version\/([\d.]+).*safari/, "se" : /msie ([\d.]+).*\.net clr (\d\.){1,2}\d+; 360se\)$/,//360浏览器 "sougou" : /msie ([\d.]+).*\.net clr (\d\.){1,2}\d+; .*metasr.*\d\)$/,//搜狗浏览器 "maxthon" : /maxthon\/([\d.]+)///遨游浏览器webkit } }; /** * 常用js代码 */ var oCommon = { /** * 改变本地对象的方法 */ base: function(){ Array.prototype.indexOf = function(vValue){ for (var i = this.length - 1; i >= 0; i--) { if (this[i] == vValue) { return i; } } return -1; }; Array.prototype.deleteIndex = function(index){ var t = this.slice(index, index + 1); this.splice(index, 1); }; Array.prototype.deleteVal = function(val){ var index = this.indexOf(val); this.deleteIndex(index); }; /** * 判断浏览器代码 */ var userAgent = navigator.userAgent.toLowerCase(); var expName = null; (expName = userAgent.match(browser.explorer.ie)) ? browser.ie = expName[1] : (expName = userAgent.match(browser.explorer.firefox)) ? browser.firefox = expName[1] : (expName = userAgent.match(browser.explorer.chrome)) ? browser.chrome = expName[1] : (expName = userAgent.match(browser.explorer.opera)) ? browser.opera = expName[1] : (expName = userAgent.match(browser.explorer.safari)) ? browser.safari = expName[1] : (expName = userAgent.match(browser.explorer.se)) ? browser.se = expName[1] : (expName = userAgent.match(browser.explorer.sougou)) ? browser.sougou = expName[1] : (expName = userAgent.match(browser.explorer.maxthon)) ? browser.maxthon = expName[1] : false; }, /** * 类选择器解决ie不支持document.getElementByClassName; * @param {Object} elem * @param {Object} selector */ classSelect: function(elem, selector){ var childNodes = elem.childNodes; var oClassEle = arguments[2] ? arguments[2] : []; for (var i = childNodes.length - 1; i >= 0; i--) { if(!childNodes[i].className)continue; if (childNodes[i].childNodes.length > 0) { oCommon.classSelect(childNodes[i], selector, oClassEle); } if (childNodes[i].className.indexOf(selector) != -1) { var thisClass = childNodes[i].className; var tmp = thisClass.split(/\s/); if (oCommon.inArray(selector, tmp)) { oClassEle.push(childNodes[i]); } } } return oClassEle; }, /** * 选择器 */ select: function(selector){ var flag = selector.charAt(0); var reg = /[a-zA-Z]/; var selector = reg.test(flag) ? selector : selector.substring(1); switch (flag) { case null: return null; break; case "": return null; break; case ".": if (document.all) { return oCommon.classSelect(document.body, selector); } else if (document.getElementsByClassName) { return document.getElementsByClassName(selector); } break; case "#": var eles = []; eles[0] = document.getElementById(selector); return eles; break; default: if(arguments[1]) return arguments[1].getElementsByTagName(selector); return document.getElementsByTagName(selector); break; } }, /** * 判断对象是否为空 * @param {Object} obj */ isEmptyObj : function(obj){ for(var i in obj){ return false; } return true; }, inArray: function(val, oArray){ for (var i = oArray.length - 1; i >= 0; i--) { if (oArray[i] === val) return true; } return false; }, /** * 兄弟节点 */ sibling: function(firstSibling, elem){ var siblings = []; for (; firstSibling; firstSibling = firstSibling.nextSibling) firstSibling.nodeType === 1 && firstSibling !== elem && siblings.push(firstSibling); return siblings }, /** * 所有兄弟节点 */ siblings: function(elem){ return oCommon.sibling(elem.parentNode.firstChild, elem); }, next : function(ele){ return ele.nextSibling; }, prev : function(ele){ return ele.previousSibling; }, parent : function(ele){ return ele.parentNode; }, first : function(ele){ return ele.firstChild; }, last : function(ele){ return ele.lastChild; }, hasChild : function(ele){ if(ele.childNodes){ return (ele.childNodes).length > 0; } }, find : function(ele,selector){ var kids = ele.childNodes; for(var i in kids){ var resultFinded = oCommon.children(kids[i],selector); var kid = kids[i]; if(kid.nodeType !== 1) continue; if(resultFinded.length > 0) { for(var k in resultFinded){ allFinded.push(resultFinded[i]); } } if(oCommon.hasChild(kid)){ oCommon.find(kid,selector); } } return allFinded; }, /** * 所有子节点或者指定子节点SSS */ children: function(parent, selector){ if (selector) { var kids = oCommon.sibling(parent.firstChild); var filterKids = oCommon.select(selector); var temp = []; for (var i = kids.length - 1; i >= 0; i--) { for (var j = filterKids.length; j >= 0; j--) { if (kids[i] === filterKids[j]) temp.push(filterKids[j]); } } return temp; } else { return oCommon.sibling(parent.firstChild); } }, child : function(parent,index){ var kids = oCommon.children(parent); if (!(typeof index === "number")) { return null; } for(var i = kids.length - 1 ; i >= 0 ; i --){ if(i === index - 1){ return kids[i]; } } return null; }, /** * 所有父节点 */ parents: function(elem, selector){ var parents = []; var parent = elem.parentNode; var filterParents = []; for (; parent; parent = parent.parentNode) { parent.nodeType === 1 && parents.push(parent); } if (selector) { filterParents = oCommon.select(selector); var temp = []; for (var i = parents.length - 1; i >= 0; i--) { for (var j = filterParents.length - 1; j >= 0; j--) { if (parents[i] === filterParents[j]) { temp.push(filterParents[j]); } } } return temp; } return parents; }, /** * 动态改变样式信息 * @param {Object} oEle dom对象 * @param {Object} style 样式信息 */ css: function(oEle){ var args = arguments; var len = args.length; switch (len) { case 1: throw new Error("请填写样式信息!"); break; case 2: var options = args[1]; if (typeof options === "object") { for (var key in options) { eval("oEle.style." + key + " = \"" + options[key] + "\""); } }else if(typeof options === "string"){ return eval("oEle.style." + args[1]); }else{ throw new Error("参数有误!"); } break; case 3: eval("oEle.style." + args[1] + " = \"" + args[2] + "\""); break; } }, html : function(ele,str){ if(!ele){ throw new Error("对象为空!"); } if(!arguments[1] && "innerHTML" in ele ){ return ele.innerHTML; }else{ if("innerHTML" in ele){ ele.innerHTML = str; }else{ throw new Error("此元素不支持innerHTML属性!"); } } }, attribute : function(ele,attrName,attrVal){ if(!attrVal){ return ele.attrName; }else{ ele.attrName = attrVal; } }, /** * 判断是否有样式 * @param {Object} oEle * @param {Object} className */ hasClass : function(oEle , className){ return oEle.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)')); }, /** * 添加样式信息 * @param {Object} oEle * @param {Object} className */ addClass : function(oEle,className){ if(! oCommon.hasClass(oEle,className)){ oEle.className += " " + className; } }, /** * 移除样式信息 * @param {Object} oEle * @param {Object} className */ removeClass : function(oEle , className){ if(oCommon.hasClass(oEle,className)){ var _thizClassName = oEle.className; _thizClassName = oCommon.trim( _thizClassName.replace(className,"")); oEle.className = _thizClassName; } }, /** * * @param {Object} oEle * @param {Object} className */ toggleClass : function(oEle , className){ if(oCommon.hasClass(oEle,className)){ oCommon.removeClass(oEle,className); }else { oCommon.addClass(oEle,className); } }, /** * 动画目前只支持 width height * @param {Object} id */ animate : function(ele){ var elem = ele, f = j = 0, callback, _this = {}, tween = function(t, b, c, d){ return -c * (t /= d) * (t - 2) + b } _this.execution = function(key, val, t){ var s = (new Date()).getTime(), d = t || 500, b = parseInt(elem.style[key]) || 0, c = val - b, a = function(){ var t = (new Date()).getTime() - s; if (t > d) { t = d; elem.style[key] = tween(t, b, c, d) + 'px'; j && callback && callback.apply(elem); return true; } elem.style[key] = tween(t, b, c, d) + 'px'; setTimeout(a, 10); } a(); } _this.animate = function(sty, t, fn){ callback = fn; for (var i in sty) { j++; _this.execution(i, parseInt(sty[i]), t); } } return _this; }, visible : function(ele){ return oCommon.css(ele,"display") !== "none"; }, /** * @param {Object} ele * @param {Object} speed */ slideDown : function(ele,speed){ var h = oCommon.css(ele, "height"); if(!oCommon.visible(ele)){ oCommon.css(ele,"height","0.000001px"); oCommon.css(ele,"display","block"); } oCommon.animate(ele).animate({ height: h }, speed, function(){ }); }, /** * @param {Object} ele * @param {Object} speed */ slideUp : function(ele,speed){ oCommon.animate(ele).animate({ height: '0px' }, speed, function(){ oCommon.css(ele,"display","none"); }); }, /** * @param {Object} ele * @param {Object} speed */ slideToggle : function(ele,speed){ if(oCommon.visible(ele)){ oCommon.slideUp(ele,speed); }else{ oCommon.slideDown(ele,speed); } }, /** * 去除前端空格 * @param {Object} str */ prevTrim : function(str){ str = str.replace(/^\s+/,""); return str; }, /** * 去除后端空格 * @param {Object} str */ lastTrim : function(str){ str = str.replace(/\s+$/,""); return str; }, /** * 去除两端、中间的所有空格 * @param {Object} str */ middleTrim : function(str){ str = str.replace(/\s+/g,""); return str; }, /** *去除两端空格 * @param {Object} str */ trim : function(str){ str = oCommon.prevTrim(str); str = oCommon.lastTrim(str); return str; }, /** * 对象循环 */ each : function(object,callback){ if(object && typeof object === "object"){ if(object instanceof Array && object.length === 0){ return; } for(i in object){ callback(i,object[i]); } }else{ throw new Error("对象不知此此方法!"); } }, /** * 生成tab * @param {Object} target * @param {Object} length */ createTab : function(tabOptions,target){ return oCommon.private.createTab(tabOptions); }, /** * ajax请求 */ ajax: function(options){ var httpRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); httpRequest.onreadystatechange = function(){ var dataType = options.dataType.toLowerCase(); httpRequest.readyState === 4 && httpRequest.status === 200 && options.callback(dataType === "json" ? eval("(" + httpRequest.responseText + ")") : dataType === "xml" ? httpRequest.responseXML : httpRequest.responseText,options.context); }; httpRequest.open(options.mode, options.url, options.sync); options.mode.toLowerCase() === "get" ? httpRequest.send(null) : httpRequest.send(options.params); }, /** * 调用某方法的target */ currentTarget: function(){ return false; }, /** * cookie的读写 */ cookie : function(){ this.flag = false; if(!this.flag){ /** * 利用正则表达式根据cookie名取得cookie值 * @param {Object} key */ oCommon.cookie.prototype.getCookie = function(key){ var arr = document.cookie.match(new RegExp("(^|\s*)" + key + "=([^;]*)(;|$)")); return arr ? decodeURIComponent(arr[2]) : null; } /** * 添加cookie * @param {Object} name * @param {Object} value * @param {Object} expires 单位为分钟 * @param {Object} path * @param {Object} domain * @param {Object} secure */ oCommon.cookie.prototype.setCookie = function(name, value, expires, path, domain, secure){ var date ; if(expires === 0){ date = new Date(0); expires = date.toUTCString() ; }else{ if(expires && (typeof expires === "number")){ date = new Date(); date.setTime(date.getTime() + expires * 60 * 1000); }else{ date = expires; } expires = expires ? date.toUTCString(): false; } document.cookie = name + "=" + encodeURIComponent(value) + ((expires) ? "; expires=" + expires : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); } /** * 删除cookie * @param {Object} key */ oCommon.cookie.prototype.delCookie = function(key){ this.getCookie(key) ? document.cookie = key + "=;expires=Thu, 1 Jan 1970 00:00:00 UTC" : false; } /** * 清空所有cookie信息 */ oCommon.cookie.prototype.clearCookie = function(){ var arr = document.cookie.split(";"); for(i in arr){ this.delCookie(arr[i].split("=")[0]); }; } this.flag = true; } }, /** * 模拟Map */ Map : function(){ this.flag = false; this.store = new Array(); if(!this.flag){ oCommon.Map.prototype.structure = function(key,val){ this.key = key; this.val = val; } oCommon.Map.prototype.put = function (key,val){ for(var i = this.store.length - 1 ; i >= 0 ; i --){ this.store[i].key === key && this.store[i].val === val ; } this.store[this.store.length] = new this.structure(key,val); } oCommon.Map.prototype.get = function(key){ for(var i = this.store.length - 1 ; i >= 0 ; i --){ if(this.store[i].key === key) return this.store[i].val; } return null; } oCommon.Map.prototype.remove = function(key){ for(var i = this.store.length - 1 ; i >= 0 ; i --){ this.store[i].key === key && this.store.splice(i,1); } } oCommon.Map.prototype.keySet = function(){ var keys = new Array(); for(var i = 0 ; i <= this.store.length - 1 ; i ++){ keys.push(this.store[i].key); } return keys; } oCommon.Map.prototype.valSet = function(){ var vals = new Array(); for(var i = 0 ; i <= this.store.length - 1 ; i ++){ vals.push(this.store[i].val); } return vals; } oCommon.Map.prototype.clear = function(){ this.store.length = 0; } oCommon.Map.prototype.size = function(){ return this.store.length; } } }, /** * 模拟StringBuilder */ StringBuilder : function(){ this.vStrings = new Array(); this.flag = false; if(!this.flag){ oCommon.StringBuilder.prototype.append = function(str){ this.vStrings.push(str); } oCommon.StringBuilder.prototype.toString = function(){ return this.vStrings.join(""); } oCommon.StringBuilder.prototype.charAt = function(index){ return this.toString().charAt(index); } oCommon.StringBuilder.prototype.clear = function(){ this.vStrings.length = 0; } oCommon.StringBuilder.prototype.Delete = function (start,end){ var tempString = this.toString(); var prevString = tempString.substring(0,start); var nextString = end ? tempString.substring(end) : tempString.substring(start+1); this.clear(); this.append(prevString); this.append(nextString); } oCommon.StringBuilder.prototype.length = function(){ return this.toString().length; } oCommon.StringBuilder.prototype.substring = function(start,end){ return this.toString().substring(start,end); } oCommon.StringBuilder.prototype.replace = function(oldStr,newStr){ var newStr = newStr ? newStr : ""; var tempString = this.toString().replace(new RegExp(oldStr,"g"),newStr); this.clear(); this.append(tempString); } oCommon.StringBuilder.prototype.indexOf = function (val){ return this.toString().indexOf(val); } oCommon.StringBuilder.prototype.lastIndexOf = function(val){ return this.toString().lastIndexOf(val); } oCommon.StringBuilder.prototype.insert = function (offset,str){ var prevString = this.substring(0,offset); var middleString = str; var nextString = this.substring(offset); this.clear(); this.append(prevString); this.append(middleString); this.append(nextString); } this.flag = true; } } }