search

Home  >  Q&A  >  body text

javascript - Use vue.js to achieve the effect: click li, get the html value of the current click li, and the parameter this becomes the window object?

The effect to be achieved:

Main code

<body>
<p id="page">
    <ul>
        <li v-on:click="aa(this)" v-for="item in items">{{item}}</li>
    </ul>
</p>
<script type="text/javascript">
new Vue({
  el: '#page',
  data: {
        items:[11,22,33,44]
  },
  methods:{
    aa:function(obj){
        console.log(obj);  //打印出来的是 window对象?
        alert(obj.html());   //找不到点击的值;
    }
  }
})
</script>
</body>

Problem Description:

The this passed after clicking li is printed as the window object, not the li object, so the object cannot be found; how to pass the current li object to js?

曾经蜡笔没有小新曾经蜡笔没有小新2762 days ago996

reply all(4)I'll reply

  • 高洛峰

    高洛峰2017-06-14 10:56:14

    Read the documentation carefully...don't make assumptions about these inexplicable usages.

    When an inline event needs to access the original event object, just add the $event parameter to it. Like this:

    <li v-on:click="aa($event)" v-for="item in items">{{item}}</li>

    To get the object of the current li tag, just read the currentTarget attribute under the event object, that is, event.currentTarget.

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-14 10:56:14

    <li @click="aa(item)" v-for="item in items">{{item}}</li>
    aa(item) {
      console.log(item) // 当前li的内容
    }

    reply
    0
  • ringa_lee

    ringa_lee2017-06-14 10:56:14

    Add a ref attribute to li and then get the dom node through this.$refs in methods

    reply
    0
  • typecho

    typecho2017-06-14 10:56:14

    Test possible:

    <li @click="aa($event)" v-for="item in items">{{item}}</li>
      methods:{
        aa:function(event){
            console.log(event.target.innerHTML);
        }
      }
    

    reply
    0
  • Cancelreply