问题
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