如何在Meteor 1.0中使用jQuery

我试图在meteor.js应用程序中使用这样的jquery。

JS:

if (Meteor.isClient) { Meteor.startup(function() { $( "button" ).click(function() { $( "p" ).toggle(); }); }); ... 

或者没有meteor.startup函数。 两者都不起作用。

HTML:

  

Can you see me?

我没有错误,单击按钮时没有任何反应。

您不应该像这样使用jQuery进行简单的事件处理,而是使用Meteor模板事件映射:

HTML:

  

JS:

 Template.myTemplate.events({ "click button":function(event, template){ template.$("p").toggle(); } }); 

使用meteor list来查看是否包含了jquery包。
如果没有,请使用meteor add jquery添加包

在启动时,您的HTML不太可能被渲染。 你想在你的templated.rendered事件上做到这一点。

但是,就像其他人说的那样,你不想那样做。

在编写应用程序代码之前,请尝试使用标准的jQuery DOM ready函数,如下所示…

 if (Meteor.isClient) { $(function() { // your jQuery code here... }); }