1

jquery パッケージが追加されたクリーンな meteor アプリケーションで、基本的な 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 parentordocumentにイベントを委譲する必要があります。existing parent

次のようにイベントをバインドできます。

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

make sure to load jQuery first.

于 2013-02-03T04:50:36.563 に答える