分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

封装自己的jquery框架

发布时间:2023-09-06 02:21责任编辑:蔡小小关键词:暂无标签

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved