Heim  >  Artikel  >  Web-Frontend  >  jquery连缀语法如何实现_jquery

jquery连缀语法如何实现_jquery

WBOY
WBOYOriginal
2016-05-16 17:47:411094Durchsuche

我想熟悉javascript的没有不知道jquery的吧,作为首屈一指的javascript框架,他的许多特性都让人兴奋不已,其中不得不提的就是特有的连缀书写语法了,那他到底只怎么实现的呢,我们也来实现一个吧.

复制代码 代码如下:

sx.$=function(id){
var t=(typeof(id)=="string"?document.getElementById(id):id);
t.text=function(){
return this.innerText?this.innerText:this.innerHTML.replace(//<.>/igm,"");
}
t.html=function(){
return this.innerHTML?this.innerHTML:null;
}
t.first=function(){
return this.firstChild?this.firstChild.nodeName!="#text"?sx.$(this.firstChild):null:null;
}
t.last=function(){
return this.lastChild?this.lastChild.nodeName!="#text"?sx.$(this.lasChild):null:null;
}
t.pre=function(){
return this.previousSibling?sx.$(this.previousSibling):null;
}
t.next=function(){
return this.nextSibling?sx.$(this.nextSibling):null;
}
t.parent=function(){
return this.parentNode?sx.$(this.parentNode):null;
}
t.setevent=function(e,f){
if(t.attachEvent){
t.attachEvent("on"+e,f);
}else{
t.addEventListener(e,f,false);
}
}
t.removeevent=function(e,f){
if(t.dettachEvent){
t.dettachEvent("on"+e,f);
}else{
t.removeEventListener(e,f,false);
}
}
t.setstyle=function(s){
var s=s.split(",");
for(var i=0;ivar s1=s[i].split(":");
this.style[s1[0]]=s1[1];
}
}
t.getstyle=function(s){
if(this.currentStyle){
return this.currentStyle[s];
}else{
return document.defaultView.getComputedStyle(this,null).getPropertyValue(s);
}
}
t.selectpath=function(m){
var m1=m;
var m=m.split("/");
var t=[];
var e=this.getElementsByTagName("*");
for(var i=0;ivar e1=e[i]
var a="";
var i1=m.length-1;
while(e1!=this){
a=e1.tagName+"/"+a;
e1=e1.parentNode;
//alert(a);
}
//alert(a);
if(m1.toLowerCase()+"/"==a.toLowerCase()){
t.push(sx.$(e[i]));
}
}
return t;
}
t.get=function(a){
return this.getAttribute(a);
}
t.set=function(a,v){
return this.setAttribute(a,v);
}
t.paste=function(h){
if(typeof(h)=="string"){
var d=document.createElement("span");
d.innerHTML=h;
}else{
var d=document.createElement("span");
d.appendChild(h);
}
var t1=this.childNodes;
for(var i=0;ialert(t1[i])
this.removeChild(t1[i]);
}
this.appendChild(d);
d.removeNode(false);
}
return t;
}
sx.$$=function(){
var t=[]
for(var i=0;it.push(sx.$(arguments[i]))
}
return t;
}

上面的代码是我最近写跨平台的javascript框架的一段核心代码,可以看出我用的是递归实现连缀语法的,在自身的方法里不断调用自身,这样实验闭包,使对象连续操作.顺便说一下,我这里对t对象用的是方法,如果是属性的话,那么在innerHTML里会显示出来的.

马上要考试了,没有太多的时间学习与研究了,虽然自己的专业和计算机无关,但还是祝愿自己不要挂课吧.
文章不足之处,还望各位多多指正.
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn