Jquery$选择器--是如何将DOM封装成jquery对象的

前言:

说到jquery不得不说的就是强大的jquery的选择器功能啦。该功能很强大,还单独分离出来sizzle模块供只需用到选择器功能的朋友使用。(该篇先不说jquery选择器的强大功能,先说说jquery是如何将DOM元素封装成jquery对象的)

一、Dom对象和jquery对象

<body>
<script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
<div id="box">测试</div>
<script>
var oBox = document.getElementById('box');
var oBox2 = document.querySelector('#box');
var Box =('#box');
console.log(oBox);
console.log(oBox2);
console.log($Box);
</script>
</body>
</html>

运行结果:

1.png

从中我们就可以看出区别了,$()把DOM对象封装成jquery对象,而DOM对象也就保存在jquery[0]中,这也就是为什么我们说的把jquery对象转化成DOM对象只需用jquery[0]或者jquery.get(0)了。

二、模拟jquery--根据id,封装jquery对象

这里先简化一下,看看封装jquery对象的一部分过程

<body>
<script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
<div id="box">测试</div>
<script>
var Box =('#box');
console.log('这是jquery对象');
console.log(Box); console.log('------分界线------'); (function(window,undefined){ var jQ = function(selector){ return new jQ.fn.init(selector); }; jQ.fn = jQ.prototype = { jquery:'2.0.0', //jquery版本号信息 constructor: jQ, //添加构造器属性 length:0, //初始length属性 selector:'', //初始selector属性 init: function(selector){ var match, elem, rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))/;
match = rquickExpr.exec( selector );
//console.log(match); //正则匹配找出id的值
if ( !selector ) { //如果selector为'',null,undefind直接退出操作
return this;
}
elem = document.getElementById(match[2]);
this[0] = elem;
this.context=document;
this.length = 1;
this.selector = selector;
return this;
}
}
jQ.fn.init.prototype = jQ.fn;
window.= jQ; })( window ); consle.log('这是模拟的对象'); console.log(('#box')); //输出封装的对象
</script>
</bod>

②输出结果:(火狐浏览器上打开的)


2.png

这里需要注意的是,chrome浏览器在显示上有会些不同


3.png

jquery显示的是类数组对象形态。

③、解析

对于上面代码有很多看不明白的朋友建议看一下我前面写的文章【jquery源码】开始学习源码之前需要解决的一些问题。

正则匹配我是直接复制了源码中的正则,可以输出该正则处理后的结果来看看。

4.png

三、模拟jquery--根据标签名,封装jquery对象

直接上代码

<body>
<ul class="list">
<li>测试1</li>
<li>测试2</li>
<li>测试3</li>
<li>测试4</li>
</ul>
<script>
console.log('这是jquery对象');
var aLi1 = $('li');
console.log(aLi1);
console.log('------分界线------');
(function(window,undefined){
var jQ = function(selector,context){
return new jQ.fn.init(selector, context);
};
jQ.fn = jQ.prototype = {
jquery:'2.0.0', //jquery版本号信息
constructor: jQ, //添加构造器属性
length:0, //初始length属性
selector:'', //初始selector属性
init: function(selector, context){
var match, elem;
if ( !selector ) { //如果selector为'',null,undefind直接退出操作
return this;
}
elem = document.getElementsByTagName(selector);
for(var i =0,len=elem.length; i<len; i++){
this[i] = elem[i];
}
this.context=document;
this.length = elem.length;
this.selector = selector;
return this;
}
}
jQ.fn.init.prototype = jQ.fn;
window.= jQ; })( window ); console.log('这是模拟的对象'); console.log(('li')); //输出封装的对象
</script>
</body>

输出结果:

5.png

这里只是单纯的模拟,jq处理起来远远没有那么简单,jquery还进行了大量的判断(下面的文章会继续说这个问题)。还可以在jquery对象中发现prevObject属性,该属性保存的是上一级的查找对象。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言: 说到jquery不得不说的就是强大的jquery的选择器功能啦。该功能很强大,还单独分离出来sizzle模...
    1263536889阅读 3,276评论 0 1
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,161评论 0 2
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,078评论 1 45
  • 单例模式 适用场景:可能会在场景中使用到对象,但只有一个实例,加载时并不主动创建,需要时才创建 最常见的单例模式,...
    Obeing阅读 6,375评论 1 10
  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 12,383评论 2 17