首页 > Web开发 > 详细

封装自己的jquery框架

时间:2018-11-10 18:35:52      阅读:139      评论:0      收藏:0      [点我收藏+]

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框架

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!