/**
* [GB2312UnicodeConverter 转码] 整数
* @param {[int]} sort [转码类型:1=中文转Unicode;其它-Unicode转中] * @param {[string]} str [要转的字符串] */1 function GB2312UnicodeConverter(str,sort){2 var sort=parseInt(sort)?parseInt(sort):0;3 if(sort){4 return escape(str).toLocaleLowerCase().replace(/%u/gi, '\\u');5 }else{6 return unescape(str.replace(/\\u/gi, '%u'));7 }8 }
/**
* [getElementsByClassName // 兼容IE7及以下] * @param {[string]} className [css样式名称] * @param {[emmet]} context [元素] * @return {[nodeList]} [节点列表] */1 function getElementsByClassName(className,context){ 2 context = context || document; 3 if(context.getElementsByName){ 4 return context.getElementsByClassName(className); 5 } 6 var nodes=context.getElementsByTagName('*'); 7 var rets=[]; 8 for(i=0;i
/**
* [setStyle 给元素加样式] * @param {[type]} el [description] * @param {[AttributeName]} prop [description] * @param {[type]} value [description] */1 function setStyle(el,prop,value){ 2 if(prop == "opacity" && !+"\v1"){ 3 //IE7 bug:filter 滤镜要求 hasLayout=true 方可执行(否则没有效果) 4 if (!el.currentStyle || !el.currentStyle.hasLayout) el.style.zoom = 1; 5 prop = "filter"; 6 if(!!window.XDomainRequest){ 7 value ="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity="+value*100+")"; 8 }else{ 9 value ="alpha(opacity="+value*100+")"10 }11 }12 el.style.cssText += ';' + (prop+":"+value);13 }
/** * [getCookie 获取cookie] * @param {[string]} c_name [description] * @return {[type]} [description] */
1 function getCookie(c_name){ 2 if (document.cookie.length>0){ 3 c_start=document.cookie.indexOf(c_name + "="); 4 if (c_start!=-1){ 5 c_start=c_start + c_name.length+1; 6 c_end=document.cookie.indexOf(";",c_start); 7 if (c_end==-1) c_end=document.cookie.length; 8 return console.log(unescape(document.cookie.substring(c_start,c_end))) 9 }10 }11 return ""12 }
1 function setCookie( name, value, expires, path, domain, secure ) { 2 var today = new Date(); 3 today.setTime( today.getTime() ); 4 if (expires) { 5 expires = expires * 1000 * 60 * 60 * 24; 6 } 7 var expires_date = new Date( today.getTime() + (expires) ); 8 document.cookie = name+'='+escape( value ) + 9 (( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()10 (( path ) ? ';path=' + path : '') +11 (( domain ) ? ';domain=' + domain : '') +12 (( secure ) ? ';secure' : '');13 }
function deleteCookie( name, path, domain ) { if (getCookie(name)) document.cookie = name + '=' + ((path)?';path='+ path:'') + ( ( domain ) ? ';domain=' + domain : '' ) + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';}
/**
* [removeNode 删除节点,解决IE删除节点后内存泄露问题] * @param {[type]} node [节点] * @return {[type]} [undefined] */1 function removeNode(node){ 2 var IE=!+"\v1"; 3 if(IE){ 4 if(node && node.tagName != 'BODY'){ 5 d = d || document.createElement('DIV'); 6 d.appendChild(node); 7 d.innerHTML = ''; 8 } 9 }else{10 if(node && node.parentNode && node.tagName != 'BODY'){11 node.parentNode.removeChild(node);12 }13 }14 }
/**
* [toggle description] * @param {[emmet]} obj [元素节点] * @return {[undefined]} [undefined] */1 function toggle(obj) {2 var el = document.getElementById(obj);3 if ( el.style.display != 'none' ) {4 el.style.display = 'none';5 }6 else {7 el.style.display = '';8 }9 }
/**
* [inArray 判断数据里是否有要测定的值] * @param {[Array]} arr [数组] * @param {[type]} value [值] * @return {[boolean]} [boolean] */function inArray(arr,value) { for (var i=0,l = arr.length ; i
/**
* [] * @return {[type]} [元素节点] */1 function $A() { 2 var elements = []; 3 for (var i = 0; i < arguments.length; i++) { 4 var element = arguments[i]; 5 if (typeof element == 'string') 6 element = document.getElementById(element); 7 if (arguments.length == 1) 8 return element; 9 elements.push(element);10 }11 return elements;12 }
/**
* [getCoords 计算元素的座标] * @param {[type]} el [元素节点] * @return {[type]} [元素所在座标] */1 function getCoords (el){ 2 var box = el.getBoundingClientRect(), 3 doc = el.ownerDocument, 4 body = doc.body, 5 html = doc.documentElement, 6 clientTop = html.clientTop || body.clientTop || 0, 7 clientLeft = html.clientLeft || body.clientLeft || 0, 8 top = box.top + (self.pageYOffset || html.scrollTop || body.scrollTop ) - clientTop, 9 left = box.left + (self.pageXOffset || html.scrollLeft || body.scrollLeft) - clientLeft;10 return { 'top': top, 'left': left };11 };
/**
* [isArray 判断对象是否为数组] * @param {[type]} obj [description] * @return {Boolean} [description] */function isArray(obj){ return Object.prototype.toString.apply(obj) === '[object Array]';} /** * [asyncInnerHTML 异部加载innerHTML插入动态内容,ie8中table新添加节点出错严重,见于IE一惯的表现,这是很正常的事!] * @param {[nodeList]} HTML [description] * @param {Function} callback [回调函数] * @return {[undefined]} [description] */function asyncInnerHTML(HTML, callback) { var temp = document.createElement('div'), frag = document.createDocumentFragment(); temp.innerHTML = HTML;//要加入的内容先放到这里。 (function(){ if(temp.firstChild) { frag.appendChild(temp.firstChild);//然后一点点挪到文档碎片 setTimeout(arguments.callee, 0);//然后把插入内容的操作作为异步调用放到一个独立的堆栈中,即使用延时 0 将操作从队列中拉出 ,防止浏览器假死; } else { callback(frag);//这里才是真正执行插入节点的操作 } })(); }/**
* [clearText 聚焦时移除文本域的默认值] * @param {[type]} field [description] * @return {[type]} [description] */function clearText(field){ if (field.defaultValue == field.value) field.value = ''; else if (field.value =='') field.value = field.defaultValue;}/** * [loadEvent 支持多个onload的事件] * @param {Function} fn [description] * @return {[type]} [undefined] */function loadEvent(fn) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = fn; }else { window.onload = function() { oldonload(); fn(); } }}/**
* [getRandomColor 生成随机颜色值] * @return {[type]} [description] */function getRandomColor(){ return '#' + (function(color){ return (color += '0123456789abcdef'[Math.floor(Math.random()*16)]) && (color.length == 6) ? color : arguments.callee(color); })('');}/**
* [getRandom 获得最小与最大间的随机数] * @param {[type]} min [description] * @param {[type]} max [description] * @return {[type]} [description] */function getRandom(min,max) { return Math.floor(Math.random()*(max-min+1))+min;}// js浏览器兼容方法
var EventUtil = { addHandler: function(element, type, handler){ if (element.addEventListener){ element.addEventListener(type, handler, false); } else if (element.attachEvent){ element.attachEvent("on" + type, handler); } else { element["on" + type] = handler; } },getButton: function(event){
if (document.implementation.hasFeature("MouseEvents", "2.0")){ return event.button; } else { switch(event.button){ case 0: case 1: case 3: case 5: case 7: return 0; case 2: case 6: return 2; case 4: return 1; } } },getCharCode: function(event){
if (typeof event.charCode == "number"){ return event.charCode; } else { return event.keyCode; } },getClipboardText: function(event){
var clipboardData = (event.clipboardData || window.clipboardData); return clipboardData.getData("text"); },getEvent: function(event){
return event ? event : window.event; },getRelatedTarget: function(event){
if (event.relatedTarget){ return event.relatedTarget; } else if (event.toElement){ return event.toElement; } else if (event.fromElement){ return event.fromElement; } else { return null; }},
getTarget: function(event){
return event.target || event.srcElement; },getWheelDelta: function(event){
if (event.wheelDelta){ return (client.engine.opera && client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta); } else { return -event.detail * 40; } },preventDefault: function(event){
if (event.preventDefault){ event.preventDefault(); } else { event.returnValue = false; } },removeHandler: function(element, type, handler){
if (element.removeEventListener){ element.removeEventListener(type, handler, false); } else if (element.detachEvent){ element.detachEvent("on" + type, handler); } else { element["on" + type] = null; } },setClipboardText: function(event, value){
if (event.clipboardData){ event.clipboardData.setData("text/plain", value); } else if (window.clipboardData){ window.clipboardData.setData("text", value); } },stopPropagation: function(event){
if (event.stopPropagation){ event.stopPropagation(); } else { event.cancelBubble = true; } }};
// js浏览器兼容方法end