我爱编程

jQuery的实质, 自己写一个jQuery

2018-01-07  本文已影响102人  Apolo_Du

jQuery 的实质:

自定义DOM API

 function getSiblings(node){
    var allChildren = node.parentNode.children
    var array = {length:0}
    for (let i =0; i<allChildren.length; i++){
    if(allChildren[i] !== item3){
      array[array.length] = allChildren[i]
      array.length += 1
    }
  }
return array
}
function addClass(node, classes){
  for(let key in classes){
      var value = classes[key]
      var methodName = value?'add':'remove'
      node.classList[methodName](key)      
    }
}
window.myDom ={}
myDom.addClass = addClass
myDom.getSiblings = getSiblings
window.Node2 = function(nodeOrSelector) {
  let node
  if (typeof nodeOrSelector === 'string') {
    node = document.querySelector(nodeOrSelector)
  } else {
    node = nodeOrSelector
  }
  return {
    getSiblings: function() {
      var allChildren = node.parentNode.children
      var array = {
        length: 0
      }
      for (let i = 0; i < allChildren.length; i++) {
        if (allChildren[i] !== item3) {
          array[array.length] = allChildren[i]
          array.length += 1
        }
      }
      return array
    },
    addClass: function(classes) {
      for (let key in classes) {
        var value = classes[key]
        var methodName = value ? 'add' : 'remove'
        node.classList[methodName](key)
      }
    }
  }
}
window.jQuery = function(nodeOrSelector) {
  let nodes = {}
  if (typeof nodeOrSelector === 'string') {
    let temp = document.querySelectorAll(nodeOrSelector)
      //我们不要nodeList, 要一个自定的类数组元素
    for (let i = 0; i < temp.length; i++) {
      nodes[i] = temp[i]
    }
    nodes.length = temp.length
  } else if (nodeOrSelector instanceof Node) {
    nodes = {
      0: nodeOrSelector,
      length: 1
    }
  }
  nodes.getText = function() {
    let text = []
    for (let i = 0; i < nodes.length; i++) {
      text.push(nodes[i].textContent)
    }
    return text
  }

  nodes.setText = function(text) {
    for (let i = 0; i < nodes.length; i++) {
      nodes[i].textContent = text
    }
    return nodes
  }
 nodes.text = function(text) {
    if (text === undefined) {
      let text = []
      for (let i = 0; i < nodes.length; i++) {
        text.push(nodes[i].textContent)
      }
      return text
    } else {
      for (let i = 0; i < nodes.length; i++) {
        nodes[i].textContent = text
      }
      return nodes
    }
  }
window.jQuery = function(nodeOrSelector) {
  let nodes = {}
  if (typeof nodeOrSelector === 'string') {
    let temp = document.querySelectorAll(nodeOrSelector)
      //我们不要nodeList, 要一个自定的类数组元素
    for (let i = 0; i < temp.length; i++) {
      nodes[i] = temp[i]
    }
    nodes.length = temp.length
  } else if (nodeOrSelector instanceof Node) {
    nodes = {
      0: nodeOrSelector,
      length: 1
    }
  }

  nodes.addClass = function(classes) {
    for (let key in classes) {
      var value = classes[key]
      var methodName = value ? 'add' : 'remove'
      for (let i = 0; i < nodes.length; i++) {
        nodes[i].classList[methodName](key)
      }
    }
  }

  nodes.text = function(text) {
    if (text === undefined) {
      let text = []
      for (let i = 0; i < nodes.length; i++) {
        text.push(nodes[i].textContent)
      }
      return text
    } else {
      for (let i = 0; i < nodes.length; i++) {
        nodes[i].textContent = text
      }
      return nodes
    }
  }
  return nodes
}
上一篇下一篇

猜你喜欢

热点阅读