【发布时间】:2021-04-26 17:11:03
【问题描述】:
我正在学习 Vue,我想将 multipe 事件绑定到 single 函数在同一元素中,如下所示(在纯 JavaScript 中,随意运行代码 sn-p):
let mainElement = document.querySelector("h1");
// I made an 'events' array to loop
["click", "mouseenter", "And we can keep adding events..."]
.forEach( event => {
mainElement.addEventListener(event, myFunction);
}
);
function myFunction() {
// DO SOMETHING, for example:
mainElement.style.color = "red";
}
const resetButton = document
.querySelector("button")
.addEventListener("click", () => {
mainElement.style.color = "black";
});
<h1 style="color: black">This is the element we want to control</h1>
<button>Reset</button>
在 Vue.js 中,我可以将 ONE SINGLE EVENT 直接绑定到这样的元素:
<h1 @mouseenter="myFunction">This is the element we want to control</h1>
我想知道是否有办法将 MULTIPLE EVENTS 绑定到同一元素内的单个函数,有人知道是否有这样的语法吗?
<h1 @[mouseenter,click,mouseleave...]="myFunction">This is the element we want to control</h1>
【问题讨论】:
标签: javascript vue.js