分享web开发知识

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

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

自己写一个jquery

发布时间:2023-09-06 01:58责任编辑:郭大石关键词:暂无标签

通过阅读jquery原代码, 我们可以模拟写一个简单的jquery

比如常用的

jQuery("div").css("color","red");
jQuery("#span1").css("color","green");
 

1. jQuery(), 因为是链式调用, 所以返回是一个对象。

 ???????jQuery = function(selector){ ???????????return new jQuery.prototype.init(selector); ???????} ???????jQuery.prototype = { ???????????constructor:jQuery, ???????????init:function(selector){ ???????????????this.elements = document.querySelectorAll(selector); ???????????}, ???????????css:function(key, value){ ???????????...... ???????????} ???????}

 此时,this.elements为所有合条件的html elements

2. css(), 对所有合适条件的element设置样式。

 ???????????css:function(key, value){ ???????????????this.elements.forEach(element => { ???????????????????element.style[key] = value; ???????????????}); ???????????}

 3. 此时,css是访问不了this.elements的。 因为this.elements是在init定义,并且init是构造函数。所以this.elements是init实例里面的属性。

 ???????jQuery.prototype.init.prototype=jQuery.prototype;

 这样 css()就能访问this.elements.

完整代码

 ???????jQuery = function(selector){ ???????????return new jQuery.prototype.init(selector); ???????} ???????jQuery.prototype = { ???????????constructor:jQuery, ???????????init:function(selector){ ???????????????this.elements = document.querySelectorAll(selector); ???????????}, ???????????css:function(key, value){ ???????????????this.elements.forEach(element => { ???????????????????element.style[key] = value; ???????????????}); ???????????} ???????} ???????jQuery.prototype.init.prototype=jQuery.prototype;

 最后起个别名

jQuery.fn=jQuery.prototype

 ???????jQuery = function(selector){ ???????????return new jQuery.fn.init(selector); ???????} ???????jQuery.fn = jQuery.prototype = { ???????????constructor:jQuery, ???????????init:function(selector){ ???????????????this.elements = document.querySelectorAll(selector); ???????????}, ???????????css:function(key, value){ ???????????????this.elements.forEach(element => { ???????????????????element.style[key] = value; ???????????????}); ???????????} ???????} ???????jQuery.fn.init.prototype=jQuery.fn;

测试一下。

<html lang="en"><head> ???<title>My jquery</title></head><body> ???<div> ???????div 1 ???</div> ???<div> ???????div 2 ???</div> ???<span id="span1"> ???????span 1 ???</span></body> ??<script type="text/javascript"> ???????jQuery = function(selector){ ???????????return new jQuery.fn.init(selector); ???????} ???????jQuery.fn = jQuery.prototype = { ???????????constructor:jQuery, ???????????init:function(selector){ ???????????????this.elements = document.querySelectorAll(selector); ???????????}, ???????????css:function(key, value){ ???????????????this.elements.forEach(element => { ???????????????????element.style[key] = value; ???????????????}); ???????????} ???????} ???????jQuery.fn.init.prototype=jQuery.fn; ???????jQuery("div").css("color","red"); ???????jQuery("#span1").css("color","green"); ??</script> </html>

自己写一个jquery

原文地址:https://www.cnblogs.com/Ivan83/p/9136390.html

知识推荐

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