4

イベントがトリガーされる方法に応じて、カスタム jQuery イベントに問題があり、委任を介してリッスンしています。文字列イベントは、両方の p タグで正しくトリガーされます。ただし、jQuery.Event オブジェクトは、直接バインドされた p タグでのみトリガーされます。デモ用にフィドルを用意しました: http://jsfiddle.net/s35bg/

HTMLは次のとおりです。

<p class="delegation">Via delegation:</p>
<p class="direct">Directly:</p>

そして JavaScript:

$(function () {
    // bind using delegation
    $(document).on('create', 'p.delegation', function (e) {
        if (e.test) {
            $(this).append(' $.Event');
        } else {
            $(this).append(' string');
        }
    });

    // bind directly
    $('p.direct').on('create', function (e) {
        if (e.test) {
            $(this).append(' $.Event');
        } else {
            $(this).append(' string');
        }
    });

    // trigger the event with a string
    $('*').trigger('create');

    // trigger the event with an object
    var event = $.Event('create');
    event.test = true;
    $('*').trigger(event);
});

結果は次のとおりです。

Via delegation: string
Directly: string $.Event

私は期待しています:

Via delegation: string $.Event
Directly: string $.Event

私の質問は、なぜですか?私の期待は間違っていますか、それともjQueryは間違っていますか? 前もって感謝します!

4

1 に答える 1