實(shí)現(xiàn)一個(gè) jQuery 的 API的過程

今天要模仿jQuery來實(shí)現(xiàn)兩個(gè)API,他們的功能是:

  1. 為選中的標(biāo)簽添加某個(gè) class
  2. 將選中的標(biāo)簽的value替換為某個(gè)text

首先,順著這個(gè)思路,寫了以下代碼:
html+css如下:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
    .red {
      color: red;
    }
  </style>
</head>
<body>
<ul>
  <li>標(biāo)簽1</li>
  <li>標(biāo)簽2</li>
  <li>標(biāo)簽3</li>
  <li>標(biāo)簽4</li>
  <li>標(biāo)簽5</li>
</ul>
</body>
</html>

一. 想到哪寫到哪
js代碼如下:

    //給所有l(wèi)i添加類名red
    let liTags = document.querySelectorAll('ul > li')
    for(let i = 0;i<liTags.length;i++){
      liTags[i].classList.add('red')    
    }
    
    //把所有l(wèi)i的內(nèi)容替換為hello
    for(let i = 0;i<liTags.length;i++){
      liTags[i].textContent = 'hello'   
    }

二. 封裝為函數(shù)
上面的代碼復(fù)用性很低,所以我們把他封裝為函數(shù),每次使用時(shí)調(diào)用就好

//給所有l(wèi)i添加類名red
    function addClass(selector,classes){
      let nodes = document.querySelectorAll(selector)
      for(let key in classes){
        let value = classes[key]
        if(value){
          for(let i = 0;i<nodes.length;i++){
            nodes[i].classList.add(key)  
          }
        }else {
          for(let i = 0;i<nodes.length;i++){
            nodes[i].classList.remove(key)  
          }
        }
      }
    }
        
    //把所有l(wèi)i的內(nèi)容替換為hello
    function setText(selector,text){
      let nodes = document.querySelectorAll(selector)
      for(let i = 0;i<nodes.length;i++){
        nodes[i].textContent = text    
      }
    }
    let classes = {'red':true,'green':true,'blue':false}
    addClass.call(undefined,'ul>li',classes)
    setText.call(undefined,'ul>li','hello')

三. 添加命名空間
當(dāng)我們自己添加的函數(shù)越來越多時(shí),會(huì)不小心把很多全局變量都給覆蓋了,所以需要命名空間

 window.myDom = {
      setClass: function(selector,classes){
        let nodes = document.querySelectorAll(selector)
        for(let key in classes){
          let value = classes[key]
          if(value){
            for(let i = 0;i<nodes.length;i++){
              nodes[i].classList.add(key)  
            }
          }else {
            for(let i = 0;i<nodes.length;i++){
              nodes[i].classList.remove(key)  
            }
          }
        }
      },
      setText: function(selector,text){
        let nodes = document.querySelectorAll(selector)
        for(let i = 0;i<nodes.length;i++){
          nodes[i].textContent = text    
        }
      }
    }
    myDom = window.myDom
   
    let classes = {'red':true,'green':true,'blue':false}
    myDom.setClass.call(undefined,'ul>li',classes)
    myDom.setText.call(undefined,'ul>li','hello')

四. 修改NodeList.prototype

NodeList.prototype.setClass = function(classes){
      for(let key in classes){
        let value = classes[key]
        if(value){
          for(let i = 0;i<this.length;i++){
            this[i].classList.add(key)  
          }
        }else {
          for(let i = 0;i<this.length;i++){
            this[i].classList.remove(key)  
          }
        }
      }
    }
    NodeList.prototype.setText = function(text){
      for(let i = 0;i<this.length;i++){
        this[i].textContent = text    
      }
    }
   
    let classes = {'red':true,'green':true,'blue':false}
    let liTags = document.querySelectorAll('ul>li')
    liTags.setClass.call(liTags,classes)
    liTags.setText.call(liTags,'hello')

五. 模仿jQuery

let jQuery = function(selector){
      let nodes = {}
      if(selector){
        nodes = document.querySelectorAll(selector)
      }
      nodes.setClass = function(classes){
        for(let key in classes){
          let value = classes[key]
          if(value){
            for(let i = 0;i<nodes.length;i++){
              nodes[i].classList.add(key)  
            }
          }else {
            for(let i = 0;i<nodes.length;i++){
              nodes[i].classList.remove(key)  
            }
          }
        }
      }
      nodes.setText = function(text){
        for(let i = 0;i<nodes.length;i++){
          nodes[i].textContent = text    
        }
      }
      return nodes
    }
    window.jQuery = jQuery
    window.$ = jQuery

   
    let classes = {'red':true,'green':true,'blue':false}
    let $liTags = $('ul>li')
    $liTags.setClass.call($liTags,classes)
    $liTags.setText.call($liTags,'hello')
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,803評(píng)論 1 45
  • 0. 寫在前面 當(dāng)你開始工作時(shí),你不是在給你自己寫代碼,而是為后來人寫代碼。 —— Nichloas C. Zak...
    康斌閱讀 5,521評(píng)論 1 42
  • 第3章 基本概念 3.1 語法 3.2 關(guān)鍵字和保留字 3.3 變量 3.4 數(shù)據(jù)類型 5種簡(jiǎn)單數(shù)據(jù)類型:Unde...
    RickCole閱讀 5,504評(píng)論 0 21
  • 1、 jQuery 能做什么? jquery是一個(gè)豐富的js庫,內(nèi)部對(duì)js的很多復(fù)雜的方法進(jìn)行了封裝和加工,比如j...
    zh_yang閱讀 1,499評(píng)論 6 13
  • 文章收錄 展望:2018,屬于數(shù)據(jù)驅(qū)動(dòng)的智能媒體時(shí)代 站在2017,預(yù)測(cè)未來媒體 我的大編輯部設(shè)想 我的中央廚房建...
    sennchi閱讀 213評(píng)論 0 0

友情鏈接更多精彩內(nèi)容