2

私はこのjQueryコードを持っています

if (Meteor.isClient){

jQuery(document).ready(function(){

    jQuery('#content .row > div').mouseenter( function(){
        jQuery('#content .row div .edit').toggle();
    });

    jQuery('#content .row > div').mouseleave( function(){
        jQuery('#content .row div .edit').toggle();
    });
});

}

アプリを実行すると、これは機能しません。それをクロムコンソールに入れると、完全に機能します。どうしたの?

これは、別のコードでも以前に発生しました。

4

2 に答える 2

4

コードは、コードの実行時に存在する DOM 要素にコールバックを追加します。ただし、Meteor は後でテンプレートをレンダリングするときにページに追加します。方法は次のとおりです。

オプション 1) Meteor イベントを使用する

Template.asdf.events({
    'click .classname': function(e) {
        ...
    }
});

オプション 2) まれに、以前の方法では機能しないものが必要な場合は、JQuery のものをrenderedコールバックに入れます。

Template.asdf.rendered = function(){
    _.each(this.findAll('.classname'), function(element){
         $(element).on('mouseenter', function(){...});
    });
};

オプション 3) すべてのページに特別な処理が必要な非常にまれなケースでは、JQuery ライブ バインディングを使用します。

Meteor.startup(function(){
    $('#content .row > div').on('click', function(){...});
});
于 2013-07-15T08:45:03.503 に答える