我是靠谱客的博主 可爱流沙,这篇文章主要介绍原生手写jquery的学习,现在分享给大家,希望可以做个参考。

jquery的编写
jQ的构造函数

复制代码
1
2
3
4
5
function jQuery(selector) { // 返回new 一个初始化函数 return new jQuery.fn.init(selector); }

显示与隐藏
定义JQuery构造函数的显示原型

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
jQuery.fn = jQuery.prototype = { constructor: jQuery, jquery: "9.0.0", length: 0, get(index) { return this[index]; }, each(callback) { for (var i = 0; i < this.length; i++) { callback(this[i]); } return this; }, click(callback) { // item指向的被遍历的每个元素 this.each(function (item) { // 让每个元素注册click事件 执行callback方法 // 也就是click 括号里面的回调函数 item.addEventListener("click", callback); }); return this; }, toggle() { this.each(function (item) { if (item.style.display != "none") { item.style.display = "none"; } else { item.style.display = "block"; } }); }, };

如何让new init 产生对象拥有JQuery显示原型上的所有方法呢?

复制代码
1
2
3
4
jQuery.fn.init.prototype = jQuery.fn; // 全局对jQuery与$可以访问 window.$ = window.jQuery = jQuery;

ready方法

复制代码
1
2
3
var isReady = false; //当前dom是否加载完毕 var readyList = []; //等待要被执行的函数礼包

监听domcontentLoade事件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
document.addEventListener("DOMContentLoaded", function () { //文档加载完毕 //alert( "DOMcontentLoaded"); //改变isReady isReady = true; //遍历readyList里直面的的数开执行 readyList.forEach((item) => item()); //做完后清空 readyList = []; }); // jq初始化函数 jQuery.fn.init = function (selector) { //如果传递的是一个函数 if (typeof selector === "function") { //如果jQuery已经准备完毕 if (isReady) { selector(); } else { //把它加入的readylist列表中 readyList.push(selector); } } else { // 获取到选择列表 var list = document.querySelectorAll(selector); // 当前对象的长度 this.length = list.length; for (var i = 0; i < list.length; i++) { //遍历类别对 this赋值 this[i] = list[i]; } } };

html编写

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="js/jquery-9.0.0.js"></script> <script> //jq中的多态 $(function () { alert("jq已经加载完毕") }) $(function () { alert("jq已经加载完毕2") }) </script> </head> <body> <h1>jquery还是大哥吗?</h1> <p>是的</p> <h1>市场占用96.8%</h1> <button>切换</button> <script> // jq实现链式操作 // 给button注册一个点击事件 // each函数编写 $("button").click(function () { // 让h1切换显示与隐藏 $("h1") .toggle() .click(function () { alert(this.innerText); }); }); $(function () { alert("jq已经加载完毕3") }) </script> </body> </html>

效果图
请添加图片描述
点击切换后
请添加图片描述
extend方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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[i] || {}; i++; } if (typeof target !== "object" && !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 && copy && (jQuery.isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) { if (copyIsArray) { copyIsArray = false; clone = src && Array.isArray(src) ? src : []; } else { clone = src && jQuery.isPlainObject(src) ? src : {}; } target[name] = jQuery.extend(deep, clone, copy); } else if (copy !== undefined) { target[name] = copy; } } } } // 返回修改后的对象 return target; }

最后

以上就是可爱流沙最近收集整理的关于原生手写jquery的学习的全部内容,更多相关原生手写jquery内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(106)

评论列表共有 0 条评论

立即
投稿
返回
顶部