摘要:本篇jQuery将讲解100行代码以内实现一个你可以工作中扩展的jquery,跟着这篇文章一起动手操作一下吧。
本篇jQuery将讲解100行代码以内实现一个你可以工作中扩展的jquery,跟着这篇文章一起动手操作一下吧。
;(function(window,factory){
factory(window);
})(window,function(window){
var jQuery=(function(){
var jQuery=function(selector){
return new jQuery.fn.init(selector);
};
jQuery.fn=jQuery.prototype={};
var init=jQuery.fn.init=function(selector){
if(!selector){
return this;
}
if(typeof selector === "string"){
if(selector.indexOf('<')!=-1){
this.ele = selector;
}else{
//选择器
this.eles = document.querySelectorAll(selector);
var len = this.eles.length;
for(var i = 0;i<len;i++){
[].push.call(this,this.eles[i]);
}
}
}else if(typeof selector === "function" ){
this.ready(selector);
}else if(typeof selector === "object"){
return this;
}
return this;
};
init.prototype=jQuery.fn;
jQuery.extend=jQuery.fn.extend=function(){
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
i = 2;
}
if (typeof target !== "object" && !jQuery.isFunction(target)) {
target = {};
}
if (i === length) {
target = this;
i--;
}
for ( ; i < length; i++) {
if ((options = arguments[ i ]) != null){
for(name in options){
src=target[name];
copy=options[name];
if(target===copy){
continue;
}
if(deep&©&&(jQuery.isPlainObject(copy)||(copyIsArray=jQuery.isArray(copy)))){
if(copyIsArray){
copyIsArray=false;
clone=src&&jQuery.isArray(src)?src:{};
}else{
clone=src&&jQuery.extend(deep,clone,copy);
}
target[name]=jQuery.extend(deep,clone,copy);
}else if(copy!==undefined){
target[name]=copy;
}
}
}
}
return target;
};
jQuery.fn.extend({
css:function(name,value){
for(var i = 0;i<this.length;i++){
this[i].style[name] = value;
}
return this;
},
appendTo:function(context){
var parentNode = document.querySelector(context).parentNode;
parentNode.insertAdjacentHTML('afterBegin',this.ele);
},
ready:function(fn){
document.addEventListener('DOMContentLoaded',fn,false);
}
});
return jQuery;
})();
window.jQuery=window.$=jQuery;
return jQuery;
});
希望这篇文章可以帮助到你。总之,同学们,你想要的职坐标IT频道都能找到!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号