命名空间

2018-07-28  本文已影响1人  _William_Zhang
命名空间 命名空间 命名空间的意义
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
    <ul>
      <li id="item1">选项1</li>
      <li id="item2">选项2</li>
      <li id="item3">选项3</li>
      <li id="item4">选项4</li>
      <li id="item5">选项5</li>
    </ul>
</body>
</html>
--------------------------------------------------------------------
/*命名空间*/
window.aWang = {}
/*添加一个获取兄弟们的方法*/
aWang.getSiblings = function(node){ /*API*/
    var allChildren  = node.parentNode.children
    var array  = {
    length:0
  }
  for(let i = 0 ;i<allChildren.length; i++){
      if(allChildren[i] !== node){
        array[array.length] = allChildren[i]
        array.length += 1
  
      }
  }
 return array

}
/*添加一个批量添加class的方法*/
aWang.addClass = function(node,classes){
  for(let key in classes){
    var value = classes[key]
    var methodName = value ? 'add' : 'remove'
        node.classList[methodName](key)
  }
}

console.log(aWang.getSiblings(item3))
aWang.addClass(item3,{'a':true,'b':true,'c':true})
上一篇 下一篇

猜你喜欢

热点阅读