简体   繁体   English

如何从 Vue.js 中的 HTML 元素获取 id

[英]How to get id from HTML Element in Vue.js

I have a Vue application, where I have several identic fontawesome logos with different id's.我有一个 Vue 应用程序,在那里我有几个具有不同 id 的相同 fontawesome 徽标。

<p @click="getId($event)" class="trash"><i id="1"  class="fas fa-trash-alt"></i></p>
<p @click="getId($event)" class="trash"><i id="2"  class="fas fa-trash-alt"></i></p>
<p @click="getId($event)" class="trash"><i id="3"  class="fas fa-trash-alt"></i></p>

Now when I click on one of those logos i execute the following method:现在,当我单击这些徽标之一时,我会执行以下方法:

getId(event){
      console.log(event.currentTarget.id);
    }

When I now click on a logo i get an empty console output.当我现在点击一个标志时,我得到一个空的控制台输出。 I googled this problem many times and also tried things like:我在谷歌上搜索过这个问题很多次,也尝试过类似的事情:

<p class="trash"><i id="1" @click="getId(this)"  class="fas fa-trash-alt"></i></p>

getId(logo){
  console.log(logo.id);
}

In this case I return a undefined.在这种情况下,我返回一个未定义的。 What could be the problem?可能是什么问题呢?

<p class="trash"><i id="1" @click="getId(this)"  class="fas fa-trash-alt"></i></p>

getId(logo){
 console.log(event.target.id);
}

as Deepak mentioned in comment all you need to do is get event target id to get value from elemen.正如迪帕克在评论中提到的,您需要做的就是获取事件目标 id 以从元素获取价值。

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

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