How to use jQuery in Meteor 1.0

a 夏天 提交于 2019-12-30 04:42:05

问题


I am trying to use jquery like this in meteor.js app.

JS:

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

Or without meteor.startup function. Neither works.

HTML:

<button>Click</button>
<p>Can you see me?</p>

I get no errors and nothing happens when I click the button.


回答1:


You shouldn't use jQuery for simple event handling like this, use Meteor templates event maps instead :

HTML :

<template name="myTemplate">
  <button type="button">Click me !</button>
  <p>Can you see me ?</p>
</template>

JS :

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



回答2:


use meteor list to see if jquery package has included.
if not, use meteor add jquery to add the package




回答3:


on startup, your HTML is not likely to be rendered. you want to do that on your templated.rendered event.

But, like others said, you don't want to do it that way anyways.




回答4:


Try using the standard jQuery DOM ready function before you write your application code as follows...

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


来源:https://stackoverflow.com/questions/27116880/how-to-use-jquery-in-meteor-1-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!