此Meteor客户端代码有两个按钮。单击CLEAR
按钮将以下内容打印到控制台:
Chrome:'footer clear'
Firefox:'form submitted'控制台也提供“ReferenceError:event not defined”
如何解决此问题,以便在所有浏览器中打印“页脚清除”? THX
Template.footer.events({
'click #clear': () => {
event.preventDefault();
console.log('footer clear'); //<---------- fired in Chrome
},
'click #info': () => {
event.preventDefault();
console.log('footer info');
}
});
Template.body.events({
'submit form': function (event) {
event.preventDefault();
console.log('form submitted'); //<---------- fired in Firefox
}
});
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="main">
<div id="content">
<form>
<button type="submit" style="display:none"></button>
{{> content}}
{{> footer}}
</form>
</div>
</div>
</body>
<template name="content">
<input type="text" id="plateNum" autocomplete="off">
</template>
<template name="footer">
<footer>
<button id="clear">CLEAR</button>
<button id="info">INFO</button>
</footer>
</template>
答案 0 :(得分:1)
您需要将事件作为参数传递给点击处理程序:
Template.footer.events({
'click #clear': (event) => {
event.preventDefault();
console.log('footer clear'); //<---------- fired in Chrome
},
'click #info': (event) => {
event.preventDefault();
console.log('footer info');
}
});
Chrome显然有一个名为&#39; event&#39;的全局变量,但Firefox对它很挑剔。