0

私はjQueryの動作を理解していないと思います。プラグイン内で、スライドコントロールのクリックイベントを処理したいのですが、検索機能を使用すると、要素を直接取得するとすべてが正常に機能します。

jQuery.fn.whoscheering = (function () {
    var el = $(this),
    left = el.find('.nav-prev'),
    right = el.find('.nav-next'),
    bubble;

    // this doesn't work
    left.bind("click", function (e) {
        console.log('left');
        e.preventDefault();
    });

    // below works!
    $('.nav-prev').bind('click', function (e) {
        console.log(e);
        e.preventDefault();
    });
});
4

1 に答える 1

0

プラグインで自己呼び出し関数を使用している場合は、次のようになります。

jQuery.fn.whoscheering = function () {
   ---
};

いいえ:

jQuery.fn.whoscheering = (function () {
   ---
});

動作しない場合.find().nav-prevおよび.nav-nextおそらくプラグインを初期化する要素内にない場合。プラグインの中にthis 、jQueryオブジェクトがあります。

jQuery.fn.whoscheering = function () {
    var left  = this.find('.nav-prev'),
        right = this.find('.nav-next'),
        bubble;

    left.on('click', function (e) {
        e.preventDefault();
        console.log('left');
    });
};

フィドル

于 2013-02-11T14:31:23.620 に答える