封装自己的jquery框架

jQuery is a fast small JavaScript library

如何封装自己的jQuery

<script>
// 这里使用沙箱模式,可以防止全局污染
(function(window,undefined){
var jQuery = function (ele){
return new jQuery.prototype.init(ele)
}
// 原型替换
jQuery.fn = jQuery.prototype ={
constructor:jQuery,
init:function(ele){
var ele = document.querySelectorAll(ele);
[].push.apply(this,ele);
},
// 这里用css()举例子
css:function(name,value){
if(arguments.length == 2){
//设置css样式
}else if(arguments.length == 1){
if( typeof name === 'object'){
// 设置多个样式
}else if(typeof name == 'string'){
// 通过getComputedStyle获取
}
}
return this;
}
}
// 最关键的一步
jQuery.prototype.init.prototype = jQuery.fn;
// 暴露给全局
window.jQuery = window.$ = jQuery;
})(window)
</script>

猜你喜欢

转载自www.cnblogs.com/sacon520/p/9940146.html