写个 jQuery选择器的简单实现和笔记

# 引入jQuery工具库

## 下载地址

- cdn:http://www.jq22.com/cdn/#a2

- 下载地址:http://www.jq22.com/jquery-info122

# 文档查询

- 中文:https://www.jquery123.com/

- 英文:https://jquery.com

 

## 引入jQuery

```js

<script src="./jQuery.js"></script>//必须放在所有引入文件的上面,防止变量冲突

```

## DOMContentLoaded 和 onload

```js

//等待所有信息加载完毕,执行下面的函数

window.onload = function(){

    console.log("window.onload");

}

//当初始的 HTML 文档被完全加载和解析完成之后

document.addEventListener("DOMContentLoaded", function(){

    console.log("DOMContentLoaded");

});

```

 

## jQuery执行函数

```js

$(function(){});

 

$(document).ready(function(){});

```

 

## $()传参

```js

$(".wapper ul");//等价于下面的写法

$("ul", ".wapper");//找到.wapper下面的ul元素

```

 

## jQuery库,原理解析

```js

//jQuery库是一个封闭作用域

//实现一个简单的jQuery库 可以选择id和class

(function () {

    //创建一个jQuery构造函数

    function jQuery(selector) {

        return new jQuery.prototype.init(selector);

    }

    //为jQuery的原型添加init属性,所有实例可以使用该属性

    jQuery.prototype.init = function (selector) {

        this.length = 0; //为this添加length属性,并且赋值为0

        //选出 dom 并且包装成jQuery对象返回

        //只判断selector是id 和 class的情况

        if (selector.indexOf('.') != -1) { //selector是class的情况

            var dom = document.getElementsByClassName(selector.slice(1));

        } else if (selector.indexOf("#") != -1) { //selector是id的情况

            var dom = document.getElementById(selector.slice(1));

        }

 

        if (dom.length === undefined) { //selector是id,返回的是一个对象,对象没有length属性

            this[0] = dom;

            this.length++;

        } else { //selector是class,返回的是一个类数组

            for (var i = 0; i < dom.length; i++) {

                this[i] = dom[i];

                this.length++;

            }

        }

    };

 

    //为jQuery的原型添加css属性,所有实例可以使用该属性

    jQuery.prototype.css = function (config) {

        for (var i = 0; i < this.length; i++) {

            for (var prop in config) {

                this[i].style[prop] = config[prop];

            }

        }

 

        return this; //链式调用的精髓

    };

 

    //上面的jQuery构造函数是new 一个jQuery.prototype.init对象,

    //jQuery.prototype.init对象上没有jQuery.prototype上的css()方法

    //所以添加下面一句,让jQuery.prototype.init对象可以调用jQuery.prototype上的css()方法

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

 

    //让外部可以通过$()或者jQuery()调用

    window.$ = window.jQuery = jQuery;

}());

```

上面是markdown格式的笔记

 

 

实现一个简单的jQuery库 可以选择id和class

(function () {
    //创建一个jQuery构造函数
    function jQuery(selector) {
        return new jQuery.prototype.init(selector);
    }
    //为jQuery的原型添加init属性,所有实例可以使用该属性
    jQuery.prototype.init = function (selector) {
        this.length = 0; //为this添加length属性,并且赋值为0
        //选出 dom 并且包装成jQuery对象返回
        //只判断selector是id 和 class的情况
        if (selector.indexOf('.') != -1) { //selector是class的情况
            var dom = document.getElementsByClassName(selector.slice(1));
        } else if (selector.indexOf("#") != -1) { //selector是id的情况
            var dom = document.getElementById(selector.slice(1));
        }

        if (dom.length === undefined) { //selector是id,返回的是一个对象,对象没有length属性
            this[0] = dom;
            this.length++;
        } else { //selector是class,返回的是一个类数组
            for (var i = 0; i < dom.length; i++) {
                this[i] = dom[i];
                this.length++;
            }
        }
    };

    //为jQuery的原型添加css属性,所有实例可以使用该属性
    jQuery.prototype.css = function (config) {
        for (var i = 0; i < this.length; i++) {
            for (var prop in config) {
                this[i].style[prop] = config[prop];
            }
        }

        return this; //链式调用的精髓
    };

    //上面的jQuery构造函数是new 一个jQuery.prototype.init对象,
    //jQuery.prototype.init对象上没有jQuery.prototype上的css()方法
    //所以添加下面一句,让jQuery.prototype.init对象可以调用jQuery.prototype上的css()方法
    jQuery.prototype.init.prototype = jQuery.prototype;

    //让外部可以通过$()或者jQuery()调用
    window.$ = window.jQuery = jQuery;
}());

调用myJquery.js:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <div id="wrapper">1</div>
    <div class="demo">2</div>
    <div class="demo">3</div>
    <script src="./myJquery.js"></script>
    <script>
        $("#wrapper").css({
            width: '100px',
            height: '100px',
            background: 'red'
        });
        $(".demo").css({
            width: '200px',
            height: '200px',
            background: 'blue'
        });
    </script>
</body>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
省市县(区)三级选择, 含UI, 可用与移动端web开发 ----------------构造函数--------------------- fnBdAdCode({ test:false,//开启测试输出 depth:3, //选择级别,1 省, 2省市, >=3省市县(区) fn:function(name,code,o){}//选择事件,返回参数 name 名称,code GB码,o 元数据{} sheng:"",//初始化查询省{sheng:"江苏省",shi:"扬州市",qu:"广陵区"} 最少要有一个参数 shi:"",//初始化查询市 qu:"",//初始化查询县(区) code:"",//初始化查询code initdom:true//加载选择弹层dom,仅查数据可不加载,手动加载 x.initdom() }); -------------初始化查询元数据------------------------ _so=fnBdAdCode({qu:"回民区",initdom:0,depth:2}); return _so: {"name":"回民区","tag":"内蒙古自治区>呼和浩特市","code":150103,"result":[{"tag":"内蒙古自治区>呼和浩特市>回民区","code":150103}]} 其中: result=[所有匹配结果] 未找到: _so.code=-1 _so.result=[] _so=fnBdAdCode({code:321000}); return _so: {"name":"扬州市","tag":"江苏省>扬州市>扬州市","code":321000,"result":[{"tag":"江苏省>扬州市>扬州市","code":321000}]} 其中: result=[所有匹配结果] 未找到: _so.code=-1 _so.result=[] -------------查询地区code------------------------ _so.getcode({sheng:"江苏省",shi:"扬州市",qu:"广陵区"}); return: 321002 未找到:-1 (_so.code=-1 _so.result=[]) so: {"name":"广陵区","tag":"江苏省>扬州市>广陵区","code":321002,"result":[{"tag":"江苏省>扬州市>广陵区","code":321002}]} 其中: result=[所有匹配结果] _so.getcode({shi:"扬州"}); return: 321000 未找到:-1 (_so.code=-1 _so.result=[]) _so: {"name":"扬州市","tag":"江苏省>扬州市>扬州市","code":321000,"result":[{"tag":"江苏省>扬州市>扬州市","code":321000}]} 其中: result=[所有匹配结果] ------------查询code对应地区------------------------- _so.gettag(150103); return: 内蒙古自治区>呼和浩特市>回民区 未找到:"" (_so.code=-1 _so.result=[]) _so: {"name":"回民区","tag":"内蒙古自治区>呼和浩特市>回民区","code":150103,"result":[{"tag":"内蒙古自治区>呼和浩特市>回民区","code":150103}]} 其中: result=[所有匹配结果] ------------列出所有省份------------------------- _so.shenglst(); return: [[{"tag":"北京市>北京市>北京市","code":110000,"name":"北京市"},{"tag":"天津市>天津市>天津市","code":120000,"name":"天津市"}],...] _so: {"name":"","tag":"","code":31,"result":[{"tag":"北京市>北京市>北京市","code":110000,"name":"北京市"},{"tag":"天津市>天津市>天津市","code":120000,"name":"天津市"}]} 其中: tag|name=都为空, code=省份数据列表长度即:result.length 未找到: code=result.length=0, 省份数据列表=result=[] -------------列出下级城市------------------------ _so.shilst(320000); return: [[{"tag":"江苏省>南京市>南京市","code":320100,"name":"南京市"},{"tag":"江苏省>无锡市>无锡市","code":320200,"name":"无锡市"}],...] _so: {"name":"江苏省","tag":"江苏省>江苏省>江苏省","code":320000,"result":[{"tag":"江苏省>南京市>南京市","code":320100,"name":"南京市"},{"tag":"江苏省>无锡市>无锡市","code":320200,"name":"无锡市"}]} 其中: tag|name|code=当前省份数据 未找到: result=城市数据列表=[] -------------列出下级县(区)------------------------ _so.qulst(321000); return: [[{"tag":"江苏省>扬州市>邗江区","code":321003,"name":"邗江区"},{"tag":"江苏省>扬州市>高邮市","code":321084,"name":"高邮市"}],...] _so: {"name":"扬州市","tag":"江苏省>扬州市>扬州市","code":321000,"result":[{"tag":"江苏省>扬州市>邗江区","code":321003,"name":"邗江区"},{"tag":"江苏省>扬州市>高邮市","code":321084,"name":"高邮市"}]} 其中: tag|name|code=当前城市数据 未找到: result=县(区)数据列表=[]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值