在添加了jquery包的干净的meteor应用程序中,我尝试使用基本的jquery css选择器。我做错了什么?不起作用的例子可以在这里找到:http://jquery-test.meteor.com/
JavaScript被放在生成的template.hello.events方法的正下方。
JS:
$("#foo").click( function() {
console.log("clicked!");
});HTML:
<button id="foo" style="border: 10px">This is a test div</button>发布于 2013-02-03 12:50:01
您必须将jQuery代码放在Template.hello.rendered函数中。
发布于 2013-02-04 06:56:18
这可能是解决问题的另一种方法:
HTML:
<button id="foo" class="foo" style="border: 10px">This is a test div</button>JS:
Template.hello.events({
 'click .foo': function(event){
    console.log('clicked');
    console.log(event.currentTarget);  
  }
});发布于 2013-02-03 12:50:37
正如你提到的,我看到了链接,你的elems are dynamically generated和你正在尝试把事件放在那些在dom中不可用的东西上。因此,您必须将事件委托给existing closest parent或document,它是所有其他元素的existing parent。
您可以像这样绑定事件:
$(document).on('click', '#foo', function() {
    console.log("clicked!");
});和make sure to load jQuery first.
https://stackoverflow.com/questions/14669541
复制相似问题