1

在添加了 jquery 包的干净流星应用程序中,我正在尝试使用基本的 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>
4

3 回答 3

2

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

于 2013-02-03T04:50:01.340 回答
1

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

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-03T22:56:18.257 回答
0

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

您可以像这样绑定事件:

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

make sure to load jQuery first.

于 2013-02-03T04:50:36.563 回答