这篇文章主要介绍了详解jquery选择器的原理的相关资料,就是jquery原型里面有一个init初始化的方法,将传入的值进行解析,比如传入的id还是class还是标签名,需要的朋友可以参考下
详解jquery选择器的原理
html部分
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<script src="js/minijquery.js"></script>
</head>
<body>
<p class="one">1</p>
<p class="two">2</p>
</body>
<script>
var result = $("p");
console.log(result);
alert($('p').size());
</script>
</html> jsjs部分
(function(){
//暴露外部的引用
var jQuery = window.jQuery = window.$ = function(selector){
return new jQuery.fn.init(selector);
}
//添加原型事件
jQuery.fn = jQuery.prototype = {
//
init:function(selector){
var element = document.getElementsByTagName(selector);
Array.prototype.push.apply(this,element);
return this;
},
myjQuery:"the test one",
length:0,
size:function(){
return this.length;
}
}
//将init的原型引用成jQuery的原型
jQuery.fn.init.prototype = jQuery.fn;
})();我用我的理解解释一下,就是jquery原型里面有一个init初始化的方法,将传入的值进行解析,比如传入的id还是class还是标签名。然后通过相应的方法返回数组型对象。既可以通过对象直接调用方法,也可以使用数组的length。
以上就是jquery选择器原理的简单介绍的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号