bind

2019-10-09  本文已影响0人  真是个非常帅气的昵称呢
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id="div">
      <span>22222</span>
      <button>获取验证码</button>
      <span>3333333</span>
  </div>
  
  <script>
    class Btn{
      constructor(id){
        this.div=document.querySelector(id)
        this.btn=this.div.querySelector('button')
        this.btn.onclick=this.show.bind(this.btn,this)
      }
      show(that){
        this.innerHTML="HHHHHHHH"
        that.div.insertAdjacentHTML('beforebegin',"span之前")
      }
    } 
    new Btn("div")
  </script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读