Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

vue如何触发某个元素的单击事件?

<a class="link">1111</a>
<a class="link">2222</a>
<a class="link">333</a>

<button>点击我触发a的单击事件</button>

在jquery中单击某个按钮后可以直接用$('a').trigger("click");
来触发a元素的单击事件

那么在vue中该如何实现?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

想通过点击<a>标签来触发事件么?这个问题我曾经遇到过,其实是vue中把a标签的点击原生事件貌似禁用掉了。但是我们可以如下这么写,来触发这个点击事件。


<a class="link" @click.native="test">1111</a>
<a class="link" @click.native="test">2222</a>
<a class="link" @click.native="test">333</a>

<button @test='shout'>点击我触发a的单击事件</button>

  <script type="text/javascript">
    var vm = new Vue({
      el:"#app",
      methods: {
        test: function () {
          console.log(1);
          this.$emit('test');//把a标签的点击事件分发
        },
        shout:function () {
          console.log('我触发了它');
        }
      }
    })
  </script>

YOU WILL NOT MISS IT~~


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...