MeteorJS基本的jQuery使用

在一个干净的meteor应用程序与添加jquery包,我试图使用基本的jQuery的CSSselect器。 我究竟做错了什么? 非工作的例子可以在这里find: 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> 

您必须将jQuery代码放在Template.hello.rendered函数内。

这可能是解决您的问题的另一种方法:

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); } }); 

正如你所提到的,我看到了链接,你的elems are dynamically generated ,你正试图把事件放在那些在dom中不可用的事件上。 因此,您必须将事件委托给existing closest parentdocument ,这是所有其他元件的existing parent

你可以像这样绑定事件:

 $(document).on('click', '#foo', function() { console.log("clicked!"); }); 

make sure to load jQuery first.