美文网首页
手写AJAX与事件委托

手写AJAX与事件委托

作者: E1FANG | 来源:发表于2019-08-22 20:45 被阅读0次

    手写AJAX

    完整版

    var xhr = new XMLHttpRequest()
    xhr.open('GET','xxx')
    xhr.onreadystatechange = function(){
        if(xhr.readyStatus ===4 ){
            if(xhr.status >= 200 && xhr.status <300 || xhr.status ==304){
                console.log('success')
            }else{
                console.log('error')
            }
        }
    }
    
    xhr.send()
    

    简洁版

    var xhr = new XMLHttpRequest()
    xhr.open('GET', 'xxx')
    xhr.onload() =function(){
        if(xhr.status >=200 && xhr.status<300 || xhr.status === 304){
            console.log('请求成功')
        }else{
            console.log('请求失败')
        }
    }
    xhr.send()
    

    手写事件代理

    $('ul').addEventListener('click', function(e){
      if(e.target.tagName.toLowerCase() === "li"){
        console.log('点击了li')
      })
    })
    ``` `

    相关文章

      网友评论

          本文标题:手写AJAX与事件委托

          本文链接:https://www.haomeiwen.com/subject/hcgosctx.html