不说了 直接上代码,以后慢慢修改和完善。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/* * 网上收集的一个类似printf的字符串格式化函数 * 调用方法很简单: element.innerHTML = String.format(’<a href=”%1″ onclick=”alert(\’%2\’);”>%3</a>’, url, msg, text); */ String.format = function(str){ var args = arguments, re = new RegExp("%([1-" + args.length + "])", "g"); return String(str).replace(re,function($1, $2){return args[$2];}); }; /*进入正题,防jQuery框架,适用于firefox,其他浏览器需要修改*/ MyQuery =unsafeWindow.MyQuery = function(selector,c){ if(this.window)return new MyQuery(selector,c); return this.init(selector,c); } MyQuery.fn=MyQuery.prototype={ test:function(){ alert(this.length); return this; }, myquery:true, version:0.01, length:-1, init:function(s,c){ if(s.myquery)return s; if(s=='body'||s==document.body){ this.length = 1; this[0]=document.body; return this; } if(typeof s == 'string'){ if(s.charAt(0)=="<" && s.charAt(s.length-1)==">" && s.length >= 3){ var div=document.createElement("div"); div.innerHTML=s; var e=div.childNodes; this.length =0; for(var i=0;i<e.length;i++){ this[this.length]=e[i]; this.length += 1; } return this; } else{ return this.find(s); } } else if(s.nodeType==1){ this.length = 1; this[0] = s; }; return this; }, find:function(s,context){ context=this.length>=0?this:[document]; var e=[]; for(i=0;i<context.length;i++){ var d=context[i].querySelectorAll(s); for(var j=0;j<d.length;j++){ if(d[j].nodeType)e.push(d[j]); } } if(context.length>1)e=MyQuery.unique(e); this.length =0; for(var i=0;i<e.length;i++){ this[this.length]=e[i]; this.length += 1; } delete e; return this; }, slice:function(){ var x=Array.prototype.slice.apply(this,arguments ); this.length=x.length; for(i=0;i<x.length;i++)this[i]=x[i]; return this; }, eq:function(i){ i=+i; return i === -1 ? this.slice(i): this.slice(i,i+1); }, get: function(num){ return num == null ? // Return a 'clean' array this.toArray(): // Return just the object ( num < 0 ? this[ this.length + num ] : this[ num ] ); }, toArray:function(){return slice.call( this, 0 );}, size:function(){return this.length;}, first:function(){return this.eq(0)}, last:function(){return this.eq(-1)}, next:function(){ var length=this.length; this.length=0; for(var i=0;i<length;i++){ var x=this[i].nextSibling; while(x && x.nodeType!=1){ x=x.nextSibling; } if(x){ this[this.length]=x; this.length++; } } return this; }, each:function(fn){ if(!(fn instanceof Function))return this; for(var i=0,j=this.length;i<j;++i){ fn.call(this[i],this[i],i,this); } return this; }, attr:function(n,v){ if(arguments.length==1){ if(this.length<=0)return ""; return (this[0].getAttribute(n))||""; } if(arguments.length==2){ for(i=0;i<this.length;i++){ this[i].setAttribute(n,v); } } return this; }, html:function(s){ if(typeof s =='string'){ for(var i=0;i<this.length;++i) this[i].innerHTML=s; return this; } if(this.length<=0)return ""; var arr=[]; for(var i=0;i<this.length;++i) arr[arr.length]=this[i].innerHTML.replace(/\r|\n|(\s{2,})/g," "); return arr.join(""); }, text:function(s){ if(typeof s =='string'){ for(var i=0;i<this.length;++i) this[i].innerText=s; return this; } if(this.length<=0)return ""; var arr=[]; for(var i=0;i<this.length;++i) arr[arr.length]=this[i].textContent.replace(/\r|\n|(\s{2,})/g," "); return arr.join(""); }, append:function(obj){ if(typeof(obj.nodeType)!='undefined')obj=obj.outerHTML; for(i=0;i<this.length;i++){ this[i].innerHTML+=obj; } return this; }, bind:function(event,fn,isCapture){ for(var i=0,j=this.length;i<j;++i){ this[i].addEventListener(event,fn,isCapture||false); } return this; }, css:function(s,d){ //驼峰转css s.replace(/([A-Z])/g,"-$1").toLowerCase(); if(arguments.length==1 && typeof(s)=='string'){ if(document.defaultView && this.length>0){ return document.defaultView.getComputedStyle(this[0], null).getPropertyValue(s); } return ""; } //设置css style if(arguments.length==2 && typeof(s)=='string' && typeof(d)=='string'){ var x=s;s={};s[x]=d; } if(typeof(s)=='object' && typeof(s.length)=='undefined'){ for(i=0;i<this.length;i++){ for(var c in s){ if(c.toLowerCase()=='float')c='cssFloat'; if(c.indexOf('-')>0)c=c.replace(/\-(\w)/g, function(all, letter){return letter.toUpperCase();}); this[i].style[c]=d; } } } return this; }, } //剔除dom元素的重复值 MyQuery.unique=function (arr){ var obj={},e=[]; for(var i=0;i<arr.length;i++){ if(!(arr[i].hasAttribute("MyQuery_id"))){ arr[i].setAttribute("MyQuery_id",Math.random()); } var id=arr[i].getAttribute("MyQuery_id"); !obj[id] && (e.push(e[i])) && (obj[id]=true); } delete obj; return e; } /*直接用greasemonkey提供的函数*/ MyQuery.ajax=GM_xmlhttpRequest; MyQuery.mask=function(str){ var id="__MyQueryMaskBackground__"; if(MyQuery("#"+this.id).size()<=0){ var style='z-index:100000;opacity:0.7;height:100%;width:100%;left:0;top:0;position:fixed;background:black'; var style1='text-align:center;z-index:100001;padding:8px;border-radius:5px;background:white;width:200px;left:600px;top:300px;position:fixed;'; var loading='http://img.alibaba.com/images/eng/wholesale/icon/loading-middle.gif'; MyQuery("body").append('<div id="'+id+'" style="display:none" >'+ '<div style="'+style+'"></div>'+ '<div style="'+style1+'"><img src="'+loading+'"></br>.'+(str||'')+'</div>'+ '</div>'); } MyQuery("#"+id).css("display",''); } MyQuery.unmask=function(){ MyQuery("#"+id).css("display",'none'); }
主要用于greasemonkey,闲着无聊自己写的。
说明:
1、将字符串转化为dom对象时如果是一个完整的html代码会出问题。
2、ajax有待完善。
3、dom节点操作函数需要添加。