欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解jquery選擇器的原理

 更新時(shí)間:2017年08月01日 15:13:50   作者:專注前端30年  
這篇文章主要介紹了詳解jquery選擇器的原理的相關(guān)資料,就是jquery原型里面有一個(gè)init初始化的方法,將傳入的值進(jìn)行解析,比如傳入的id還是class還是標(biāo)簽名,需要的朋友可以參考下

詳解jquery選擇器的原理

html部分

<!doctype html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8" /> 
  <title>Document</title> 
  <script src="js/minijquery.js"></script> 
</head> 
<body> 
  <div class="one">1</div> 
  <div class="two">2</div> 
</body> 
<script> 
  var result = $("div"); 
  console.log(result); 
  alert($('div').size()); 
</script> 
</html> js 

js部分

(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原型里面有一個(gè)init初始化的方法,將傳入的值進(jìn)行解析,比如傳入的id還是class還是標(biāo)簽名。然后通過相應(yīng)的方法返回?cái)?shù)組型對(duì)象。既可以通過對(duì)象直接調(diào)用方法,也可以使用數(shù)組的length。

以上就是jQuery 原理的分析,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論