1.根据id名称取元素 $id(idName) 2.根据class定义取元素 $class(className)返回所有class被定义成className的元素数组,或者$Eclass(className)返回第一个元素 3.根据tag名称取元素 $tag(tagName)返回所有标签名称为tagName的元素数组,或者$Etag(tagName)返回第一个元素 4.父子关系下取元素 $dom(id#idName class#className tag#tagName)返回idName元素下的所有class为className的所有元素的标签名称为tagName的元素数组,或者$Edom(id#idName class#className tag#tagName)返回第一个元素,每个关系之间以空格分隔,对应关系以井号分隔,关系之间可任意顺序 5.取出来的元素会附带有扩展方法 a.getAttr(attrName) 取属性 b.setAttr(attrName, attrValue) 设置属性 c.getStyle(styleName) 取样式 d.setStyle(styleName,styleValue) 设置样式 e.getVal() 取值,如果是input,select,textarea取出来的是value的值,其它标签的话取出来的是内嵌html f.setVal(value) 设置值, 如果是input,select,textarea设置的是value的值,其它标签的话设置的是内嵌html g.show() 显示元素 h.hide() 隐藏元素 5.AJAX操作使用函数,ajax(type,url,callback,data); a.type 提交类型 b.url 提交地址 c.callback 回调函数 d.data 待提交数据(a=1&b=2) 6.Cookie操作使用函数,cookie.set(k,v,e) k名称 v值 e有效期(秒) cookie.get(k) 1. [文件] JSTools.js var JSTools = {version:1.0,author:'lymz',email:'[email protected]'};var $id = function(id){ return $extend(document.getElementById(id));};var $class = function(clas){ return $extend(getElementsByClassName(clas));};var $Eclass = function(clas){ var t = $class(clas); return t.length > 0 ? t[0] : null;};var $tag = function(tag){ return $extend(document.getElementsByTagName(tag));};var $Etag = function(tag){ var t = $tag(tag); return t.length > 0 ? t[0] : null;};var $dom = function(dom){ var t = dom.split(' '); var p=null; for(var i = 0,l = t.length;i if(i > 0 && !p) break; else if(i > 0 && p) { if(p.length != undefined) p = p[0]; } var tt = t[i].split('#'); if(tt[0] == 'id') p = $id(tt[1]); else if(tt[0] == 'class') { tt[1] = tt[1].split('|'); if(p) p = getElementsByClassName(tt[1][0],p); else p = $class(tt[1][0]); if(tt[1][1]) p = $filter(p,tt[1][1]); } else if(tt[0] == 'tag') { tt[1] = tt[1].split('|'); if(p) p = p.getElementsByTagName(tt[1][0]); else p = $tag(tt[1][0]); if(tt[1][1]) p = $filter(p,tt[1][1]); } else p = null; } return $extend(p);};var $filter = function(p,filter){ if(p == null) return null; var tt = new Array(); if(filter.indexOf('=') !== -1) { var t = filter.split('='); for(var i = 0,j = 0,l = p.length;i if(p[i].getAttribute(t[0]) == t[1]) tt[j++] = p[i]; } } else if(filter.indexOf('^') !== -1) { var t = filter.split('^'); for(var i = 0,j = 0,l = p.length;i if(p[i].getAttribute(t[0]).toLowerCase() != t[1].toLowerCase()) tt[j++] = p[i]; } } else { for(var i = 0,j = 0,l = p.length;i if(p[i].nodeName.toLowerCase() == filter.toLowerCase()) tt[j++] = p[i]; } } return tt;};var $Edom = function(dom){ var t = $dom(dom); if(t != null && t.length != undefined) return t[0]; else return t;};var $extend = function(object){ if(!object) return object; var t = false; if(object.length == undefined){ object = new Array(object); t = true; } for(var i = 0,l = object.length;i object[i].getAttr = function(attr){return this.getAttribute(attr);}; object[i].setAttr = function(k,v){return this.setAttribute(k,v);}; object[i].getStyle = function(k){return eval('this.style.'+k);}; object[i].setStyle = function(k,v){return eval('this.style.'+k+'=\''+v+'\'');}; object[i].getVal = function(){return this.value!=undefined?this.value:this.innerHTML;}; object[i].setVal = function(v){if(this.value!=undefined)this.value=v;else this.innerHTML=v;return true;}; object[i].show = function(){return this.setStyle('display','block');}; object[i].hide = function(){return this.setStyle('display','none');}; }http://www.enterdesk.com/special/meitu/ object.each=function(func){美图 if(this.length == undefined) object = new Array(object); var it = null; for(var i = 0,l = object.length;i it = object[i]; func(it,i); } }; return t ? object[0] : object;};var ajax = function(type,url,func,data){ if(window.ActiveXObject){ var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else { var xmlHttp=new XMLHttpRequest(); } xmlHttp.open(type,url,true); xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState == 4) if(xmlHttp.status == 200) if(func) func(xmlHttp.responseText); }; xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xmlHttp.send(data?data:null);};var cookie = { get:function(k){ var t = document.cookie.split('; '); for(var i = 0,l = t.length;i var tt = t[i].split('='); if(tt[0] == k) { return decodeURI(tt[1]); } } return undefined; }, set:function(k,v,e){ if(e) { var d = new Date(); d.setTime(d.getTime()+(e*1000)); e = ';expires='+d.toGMTString(); } else e = ''; document.cookie = k + '=' + v + e; }};var getElementsByClassName = function(c,p){ p = p ? p : document; var arrElements = p.getElementsByTagName('*'); var arrReturnElements = new Array(); c = c.replace(/\-/g, "\\-"); var oRegExp = new RegExp("(^|\\s)" + c + "(\\s|$)"); var oElement; for(var i=0; i oElement = arrElements[i]; if(oRegExp.test(oElement.className)){ arrReturnElements.push(oElement); } } return arrReturnElements;}; 10-20 22:26