我想通过 this.id
参数传递 id
但我无法发送和接收它。如何在 vue.js 上发送 this.id
作为参数?
<button
id="txt"
@click="activate(this.id)"
class="kind-btn"
type="button"
>
<i class='fas fa-poll-h kind-icon'></i>
</button>
<button
id="pic"
@click="activate(this.id)"
class="kind-btn"
type="button"
style="padding-left: 15px;"
>
<i class='fas fa-image kind-icon'style="font-size: 30px;"></i>
</button>
<button
id="sound"
@click="activate(this.id)"
class="kind-btn"
type="button"
>
<i class="material-icons kind-icon"style="font-size: 31px;top: 8px;margin-bottom: 13px;">music_video</i>
</button>
//js
activate: function(i) {
console.log(i);
switch (i) {
case "txt":
$("#" + this).css({"background-color": "#fe5d87", "color": "white"});
break;
case "pic":
$("#" + this).css({"background-color": "#fe5d87", "color": "white"});
break;
case "sound":
$("#" + this).css({"background-color": "#fe5d87", "color": "white"});
break;
//
// default:
// $("#txt").css({"background-color": "#fe5d87", "color": "white"});
}
最佳答案
https://v2.vuejs.org/v2/guide/events.html
您可以执行@click="activate"
methods: {
activate: function (event) {
console.log(event.target.id)
}
}
如果您需要传递更多数据,另一种方法是传递事件
@click="activate('your custom data string', $event)"
关于javascript - 如何在 vue js 上传递 id @click ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68966924/