简体   繁体   English

Vue @click不适用于存在href的锚标记

[英]Vue @click doesn't work on an anchor tag with href present

EDIT: I forgot to clarify, what I'm looking for is to know how to write an anchor tag with the href attribute present but that when the element is clicked, the href should be ignored and the click handler should be executed. 编辑:我忘了澄清,我要寻找的是知道如何编写具有href属性存在的锚标记,但是当单击元素时,应忽略href并应执行click处理程序。

I'm currently using Vue 1 and my code looks like: 我目前正在使用Vue 1,我的代码如下所示:

<div v-if="!hasPage(category.code)">
  <div>
    <template v-for="subcategoryList in subcategoryLists[$index]" >
      <ul>
        <li v-for="subcategory in subcategoryList"v-on:click.stop="test()">
          <a :href="subcategory.url">{{subcategory.label}}</a>
        </li>
      </ul>
    </template>
  </div>
</div>

What i'm trying to do is present the user with some buttons that represent my site's categories, some of this buttons will lead the user to another page and other will toggle a dropdown with a list of subcategories, when clicking these subcategories you will be taken to the subcategory's page. 我正在尝试为用户提供一些按钮,这些按钮代表我的网站类别,其中一些按钮会将用户引导到另一个页面,其他按钮将在带有子类别列表的下拉菜单中切换,单击这些子类别后,您将转到子类别的页面。

These would be pretty simple but these buttons need to be tracked by Google Tag Manager so that's why I used the @click attribute to call a function and ignore the href attribute. 这些非常简单,但是这些按钮需要由Google跟踪代码管理器进行跟踪,因此这就是为什么我使用@click属性调用函数并忽略href属性的原因。 This doesn't work, I have tried @click.prevent, @click.stop, @click.stop.prevent and none of these work. 这不起作用,我尝试了@ click.prevent,@ click.stop,@ click.stop.prevent,但这些都不起作用。

The thing is that if I remove the href attribute, the @click method works great but an SEO requirement is to have href attributes on every link. 关键是,如果我删除href属性,则@click方法效果很好,但SEO要求是在每个链接上都具有href属性。

Any help would be appreciated. 任何帮助,将不胜感激。

As @dan-oswalt specified in the comments, you have to add the click event on the same element as the href . 正如注释中指定的@ dan-oswalt一样,您必须在与href相同的元素上添加click事件。 The reason it did not work the time you tried is most likely because you tried with @click.stop which only stops propagation, not the default action. 它在您尝试的时间无效的原因很可能是因为您使用@click.stop尝试过,它只停止传播,而不是默认操作。 (Redirect to the link). (重定向到链接)。 What you want is to prevent the browser to redirect the user: @click.prevent 您想要的是防止浏览器重定向用户: @click.prevent

 new Vue({ el: '#app', data: { subcategoryList: Array(10).fill({ url: 'http://google.com', label: 'http://google.com' }) }, methods: { test(event) { event.target.style.color = "salmon" } } }) Vue.config.devtools = false Vue.config.productionTip = false 
 <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script> <main id="app"> <template> <ul> <li v-for="subcategory in subcategoryList"> <a :href="subcategory.url" v-on:click.prevent="test($event)">{{subcategory.label}}</a> </li> </ul> </template> </main> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM