我目前使用的是 Meteor 0.6.3
以下使用“id”作为选择器的情况不起作用:
模板:
<template name="menu">
<div>
<button id="showmap" class="btn-primary">Show map</button>
</div>
</template>
JS:
Template.menu.events({
'click #showmap' : function () {
alert("test");
}
});
如果我使用“class”而不是“id”一切正常:
模板:
<template name="menu">
<div>
<button class="btn-primary showmap">Show map</button>
</div>
</template>
JS:
Template.menu.events({
'click .showmap' : function () {
alert("test");
}
});
我见过几个使用“id”作为选择器的例子。那么我可能做错了什么?
最佳答案
我试过你的代码,但它不像你说的那样工作。奇怪的是类选择器的事件也不起作用。但是,我添加了很多软件包,所以如果其中一个出现干扰,我不会感到惊讶。
我在 SO 上发现了对一个非常相似的问题的回应: Setting simple events in meteor
因此您可能想要搜索错误报告并发布它(如果尚未发布)。 https://github.com/meteor/meteor/issues
最好的解决方法可能是使用类作为选择器,然后针对 event.currentTarget.id
进行测试,它应该具有被单击元素的 ID。
关于javascript - 我如何使用元素的 id 来触发 Meteor.js 中的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16784484/