实现一个 jQuery 的 API

实现一个 jQuery 的 API

<!--html代码-->
<body>
  <h1>text</h1>
  <ul>
    <li id="item1">选项1</li>
    <li id="item2">选项2</li>
    <li id="item3">选项3</li>
    </ul>
</body>

封装函数

  • 获取目标元素的所有兄弟元素
//javaScript代码
function getSiblings(node) {
  var allChlidren = node.parentNode.chlidren
  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
}
//调用方法
getSiblings(item3)
  • 给目标元素添加class
//javaScript代码
function addClass(node,classes){
  for (let key in classes){
    var value = classes[key]
    var methodName = value? ' add ' : ' remove ' 
    node.classList[methodName](key)
  }
}
// 调用addClass()
addClass ( node,{a:true , b:false,c:true } )

命名空间

//javaScrip代码
var  dom={}
dom.getSiblings=function (node) {
  var allChlidren = node.parentNode.chlidren
  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
}

dom.addClass = function (node,classes){
  for (let key in classes){
    var value = classes[key]
    var methodName = value? ' add ' : ' remove ' 
    node.classList[methodName](key)
  }
}

//调用方法
dom.getSiblings( node )
dom.addClass ( node,{a:true , b:false,c:true } )

扩展 Node 接口

  • 直接在 Node.prototype 上加函数
Node.prototype.getSiblings =function () {
  var allChlidren = this.parentNode.chlidren
  var array = { 
    length:0 
  }
  for(let i=0;i<allChildren.length;i++){
    if(allChildren[i] !== this){
    array[array.length] =allChildren[i]
    array.length += 1
    }
  }
  return array
}


Node.prototype.addClass = function (classes){
 	 for (let key in classes){
    	 var value = classes[key]
   		 var methodName = value? ' add ' : ' remove ' 
    	 this.classList[methodName](key)
  }
}
  • 无侵入,新的接口
window.Node2 =  function( nodeOrSelector ){
    let nodes = { }
    if( typeof nodeOrSelector === 'string' ){
        let temp =  document.querySelectorALL ( nodeOrSelector)
        for(let i=0 ; i<temp.length ; i++ ){
            nodes[i] = temp[i]
        }
        node.length = i 
    }else if (nodeOrSelector instanceof Node){
        nodes = {
        0:nodeSelector , 
        length:1
        }
    }
    nodes.addClass = function ( classes ) {
        classes.forEach( (value) => {
            for( let i=0 ; i< nodes.length ; i++ ){
                nodes[i].classList.add(value)
            }
        })
    } 
    nodes.getText = function ( ){
        var texts= [ ]
        for ( let i=0 ; i<nodes.length ; i++ ){
        texts.push(nodes[i].textContent)
    }
    nodes.setText = function (text){
        for ( let i=0 ; i<nodes.length ; i++ ){
            nodes[ i ] .textContext = text
        }
    }
    return nodes
}
  • 改个名字
Node2改成jQuery
  • 给个缩写
window.$ = jQuery

至此,实现了一个 jQuery 的 API

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值