let var作用域

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <p>{{ message }}</p>

      <button type="button" v-on:click="getNum()">1</button>
      <button type="button" v-on:click="getNum()">2</button>
      <button type="button" v-on:click="getNum()">3</button>
    </div>

    <script>
      const app = new Vue({
        el: '#app',
        data: {
          message: 'Hello Vue.js!',
          firstName: "thomas",
          lastName: "zhang",
        },
        methods: {
          getNum: function() {
            const btns = document.getElementsByTagName('button');
            for (var i = 0; i < btns.length; i++) {
              btns[i].addEventListener('click', function() {
                console.log('button ' + i + ' click');
              })
            }

            for (let i = 0; i < btns.length; i++) {
              btns[i].addEventListener('click', function() {
                console.log('button ' + i + ' click');
              })
            }
          }
        },
        computed: {}
      })
    </script>
  </body>
</html>

let var作用域

上一篇:js对整个div进行侦听,并通过设定值判断是哪个按钮的点击事件


下一篇:Angular 中的 dom 操作(ViewChild)以及父子组件中通过 ViewChild 调用子组件的方法