首页 > WEB扩展 > jQuery > 正文

JQuery教程:简化JQuery

2020-09-19 10:57:20
字体:
来源:转载
供稿:网友
这是以前整理的一些代码,与jqer分享,希望对大家能有些帮助。

话说功能愈发强大的jquery体积也不再小巧,55k(minified)的大小虽然不能说很大,但如果用在一些小型项目或网站上,也不算小,那么我们何不根据自己项目特点,简化或改写jquery呢,follow me!

首先是jquery的核心代码:
代码:
//不完全一致,但实现手段大致如此
(function(){
  window._$ = window.$;
  var $ = window.$ = function(s) {
    return new $.fn.init(s);
  };
  $.fn = $.prototype = {
    init: function(s) {
      if(!s) return this;
      if (s.nodetype) {
        this.e = [];
        this.e.push(s);  //存储获取到的elements      
        return this;
      }
      if (typeof s == “string”) {
        return $().find(s);
      }
      return this;
    },
    find: function(s) {
      //根据传入的string,查找dom
    }
  };
  // extend扩展方法
  $.fn.extend = function(p) {
    for(var key in p) {
      if(!$.fn[key]) {
        $.fn[key] = p[key];
      }
    }
  };
  $.fn.init.prototype = $.fn;
})();
$().extend({
  “a”: function(s){},
  “b”: function(s){}
});
再附上我简化的版本
代码:
// by cnwander
(function(){
window._$ = window.$;
var eleexpr =  /([#/.a-za-z])([^/s]+)/g;
var $ = window.$ = function(s) {
     return new $.fn.init(s);
};
$.fn = $.prototype = {
  init: function(s) {
   this.e = null;
   if(!s) return this;
   if (s.nodetype) {
    this.e = [];
    this.e.push(s);
    return this;
   }
   if (typeof s == “string”) {
    return $().find(s);
   }
   else
    return this;
  },
  find: function(s) {
   var wrap = this.e || [document];
   var result = [];
   for(var key in wrap) {
    var target = wrap[key];
    while(eleexpr.test(s)) {
     var first = regexp.$1,
      content = regexp.$2;
     target = $().clean(target,content,first);
    }
    if(target == null)
     result = null;
    else
     for(var i = 0; i < target.length; i++)
      result.push(target[i]);
   }
   this.e = result;
   return this;   
  },
  clean: function(wrap,content,type){
   if(!wrap) return null;
   wrap = wrap instanceof array ? wrap : [wrap];
   var result = new array();
   for(var key in wrap) {
    var temp;
    switch(type) {
    case “#”:
     temp = wrap[key].getelementbyid(content);
     break;
    case “.”:
     temp = $().getelemsbyclassname(content,wrap[key]);
     break;
    default:
     temp = wrap[key].getelementsbytagname(type+content);
    }
    if(temp) {
     temp = temp.length ? temp : [temp];
     for (var i = 0; i < temp.length; i++)
      if(temp[i].nodetype) result.push(temp[i]);
    }
   }
   result = result.length <= 0 ? null : result;
   return result;
  },
  getelemsbyclassname: function(classname,elem,tag) {
   tag = tag || “*”;
   elem = elem || document;
   elem = elem instanceof array ? elem : [elem];
   var result = new array();
   for(var key in elem) {
    var allelems = elem[key].getelementsbytagname(tag) || elem[key].all;
    var oelem;
    for(var i=0; i<allelems.length; i++){
     oelem = allelems[i];
     var list = oelem.classname.split(” “);
     for(var j=0; j<list.length; j++){
      if(list[j] == classname) result.push(oelem);
     }      
    }
   }
   return result.length <= 0 ? null : result;
  }
};
$.fn.extend = function(p) {
  for(var key in p) {
   if(!$.fn[key]) {
    $.fn[key] = p[key];
   }
  }
};
$.fn.init.prototype = $.fn;
})();
$.ajax = function(url,poststr,lastfunc,errfunc) {
var ajax = false;
if(window.xmlhttprequest) {
  ajax = new xmlhttprequest();
  if (ajax.overridemimetype) {
   ajax.overridemimetype(”text/xml”);
  }
}
else if (window.activexobject) {
  try {
   ajax = new activexobject(”msxml2.xmlhttp”);
  }
  catch (e) {
   try {
      ajax = new activexobject(”microsoft.xmlhttp”);
    }
   catch (e) {
   }
  }
}
if (!ajax) {
  if(errfunc) errfunc();
  return false;
}
ajax.open(”post”, url, true);
ajax.setrequestheader(”content-type”,”application/x-www-form-urlencoded”);
ajax.send(poststr);
ajax.onreadystatechange = function() {
  if (ajax.readystate == 4 && ajax.status == 200) {
   if(lastfunc) lastfunc(ajax.responsetext);
  }
}  
}
$().extend({
”html”: function(val){
  return val == undefined ?
  (this.e[0] ?
   this.e[0].innerhtml :
   null) :
  this.e[0].innerhtml = val;
},
”empty”: function() {
  for(var i in this.e) {
   var o = this.e[i];
   o.innerhtml = ”;
  }
  return this;
},
”css”: function() {
  if(!this.e || arguments.length <= 0) return this;
  if(arguments.length == 1 && typeof arguments[0] == “string”) {
   arguments[0] = arguments[0].tolowercase();
   return this.e[0].currentstyle ?
   this.e[0].currentstyle[arguments[0]] :
   window.getcomputedstyle (this.e[0], “”).getpropertyvalue(arguments[0].replace(/([a-z])/g, “-$1′));
  }
  else if(arguments.length >= 2) {
   for (var i in this.e)
   this.e[i].style[arguments[0]] = arguments[1];
  }
},
”hasclass”: function(name) {
  if(!this.e) return this;
  var allclass =  this.e[0].classname.split(” “);
  for (var key in allclass)
  if(allclass[key] == name) return true;
  return false;
},
”addclass”: function(name){
  if(this.e)
   for (var i in this.e)
    if(this.e[i].nodetype == 1)
     this.e[i].classname += ” “+name;
  return this;
},
”removeclass”: function(name){
  if(this.e)
   for (var i in this.e) {
    var temp = [],
     allclass =  this.e[i].classname.split(” “);
    for(var j = 0, k = 0; j < allclass.length; j++) {
     if(allclass[j] != name) {
      temp[k++] = allclass[j]
     }
    }
    allclass = temp.join(” “);
    this.e[i].classname = allclass;
   }
  return this;
},
”mousedown”: function(callback){
  if(!this.e) return this;
  for(var key in this.e) {
   this.e[key].onmousedown = callback;
   return this;
  }
},
”mouseover”: function(callback){if(!this.e) return this;for(var key in this.e) {this.e[key].onmouseover = callback} return this},
”mouseout”: function(callback){if(!this.e) return this;for(var key in this.e) {this.e[key].onmouseout = callback}return this}
});
才疏学浅,错误之处,敬请指正。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表