自己写一个jquery

通过阅读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>

 

转载于:https://www.cnblogs.com/Ivan83/p/9136390.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值