码迷,mamicode.com
首页 > Web开发 > 详细

封装自己的jquery框架

时间:2018-11-10 19:12:08      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:return   new   tor   this   javascrip   undefined   define   ring   app   

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>

封装自己的jquery框架

标签:return   new   tor   this   javascrip   undefined   define   ring   app   

原文地址:https://www.cnblogs.com/sacon520/p/9940146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!