Jquery初探

  var node = {}
  if (typeof nodeOrSelector === 'string') {
    var temp = document.querySelectorAll(nodeOrSelector);
    for (var i = 0; i < temp.length; i++) {
      node[i] = temp[i]
    }
    node.length = temp.length
  }else if(nodeOrSelector.nodeType === 1){
    node[0] = nodeOrSelector
    node.length = 1
  }
  node.getsiblings = function() {
    var childrenAll = node[0].parentNode.children;
    var array = {
      length: 0
    };
    for (var i = 0; i < childrenAll.length; i++) {
      if (childrenAll[i] !== node[0]) {
        array[array.length] = childrenAll[i];
        array.length += 1;
      }
    }
    return array;
  }
  node.addClass = function(array) {
    for (key in array) {
      var methodName
      methodName = array[key] ? 'add' : 'remove';
      for (var i = 0; i < node.length; i++) {
        node[i].classList[methodName](key);
        console.log('1')
      }
    }
  }
  node.text = function(text) {
    if (text !== undefined) {
      for (var i = 0; i < node.length; i++) {
        node[i].textContent = text
      }
    } else {
      console.log(node)
      var texts = []
      for (var i = 0; i < node.length; i++) {
        texts.push(node[i].textContent)
      }
      return texts
    }
  }
  return node
}

window.$ = jquery;
var $li = $(items2);
$li.addClass({
  blue: true,
})
console.log($li.getsiblings())

node.querySelectorAll获取到的是一个伪数组,为了以后使用方便应该变成一个简单的对象。

一个对象length是必须设置的项目,否则很多循环取值会出错

你可能感兴趣的:(Jquery初探)