本文摘自PHP中文网,作者jacklove,侵删。
前言:说到jquery不得不说的就是强大的jquery的选择器功能啦。该功能很强大,还单独分离出来sizzle模块供只需用到选择器功能的朋友使用。(该篇先不说jquery选择器的强大功能,先说说jquery是如何将DOM元素封装成jquery对象的)一、Dom对象和jquery对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <body>
<script src= "https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js" ></script>
<p id= "box" >测试</p>
<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>
|
运行结果:

从中我们就可以看出区别了,$()把DOM对象封装成jquery对象,而DOM对象也就保存在jquery[0]中,这也就是为什么我们说的把jquery对象转化成DOM对象只需用jquery[0]或者jquery.get(0)了。
二、模拟jquery--根据id,封装jquery对象
这里先简化一下,看看封装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 34 35 36 37 38 39 40 41 42 43 44 | <body>
<script src= "https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js" ></script>
<p id= "box" >测试</p>
<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' ,
constructor: jQ,
length:0,
selector: '' ,
init: function (selector){
var match, elem, rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*| #([\w-]*))$/;
match = rquickExpr.exec( selector );
if ( !selector ) {
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 );
console.log( '这是模拟的对象' );
console.log($$( '#box' ));
</script>
</body>
|
②输出结果:(火狐浏览器上打开的)

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

jquery显示的是类数组对象形态。
③、解析
对于上面代码有很多看不明白的朋友建议看一下我前面写的文章【jquery源码】开始学习源码之前需要解决的一些问题。
正则匹配我是直接复制了源码中的正则,可以输出该正则处理后的结果来看看。

三、模拟jquery--根据标签名,封装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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <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' ,
constructor: jQ,
length:0,
selector: '' ,
init: function (selector, context){
var match, elem;
if ( !selector ) {
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>
|
输出结果:

这里只是单纯的模拟,jq处理起来远远没有那么简单,jquery还进行了大量的判断(下面的文章会继续说这个问题)。还可以在jquery对象中发现prevObject属性,该属性保存的是上一级的查找对象。看看下面的例子就能明白了。
<body>
<script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
<ul class="list">
<li>测试1</li>
<li>测试2</li>
<li>测试3</li>
<li>测试4</li>
</ul>
<script>
var aLi1 = $('li');
console.log(aLi1);
var aLi2 = $('li','.list');
console.log(aLi2);
var aLi3 = $('.list').find('li');
console.log(aLi3);
</script>
</body>

本文讲解了$选择器--是如何将DOM封装成jquery对象,更多相关内容请关注php中文网。
相关推荐:
css3动画导航栏3D
requests库的基本使用
前端调用微信支付接口
以上就是$选择器--是如何将DOM封装成jquery对象的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
jquery中dom操作――wrap
dom节点如何进行优化
$选择器--是如何将dom封装成jquery对象
javascript中dom的含义是什么
dom是什么意思
html dom是什么
怎么遍历dom
浅谈javascript之dom核心操作
javascript操作dom的方法有哪些?
javascript的三大组成部分
更多相关阅读请进入《dom》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » $选择器--是如何将DOM封装成jquery对象