开发者

jquery, trigger, bind, strange parameter

I have the following code:

<body>
    <form>
        <input type="text"/>
    </form>

    <script>

        $( function () {

            $(document).bind("EVENT", function (event, element) {
                console.log("BIND", element, event);
            });

            $("form").each( function iterate(index, element) {

                console.log("BEFORE BIND", element);
                $(document).trigger("EVENT", element)
            });
        })
    <开发者_StackOverflow中文版/script>
</body>

I expect that the element that i pass to the TRIGGER is the same as i get at the BIND but no

BEFORE BIND: this's the FORM, as it's expected

BIND: this is the INPUT box, no idea why

is it a bug or i miss something?

best, Viktor


If I understand your question correctly this should be what you are looking for:

$(function () {
    $(document).bind("EVENT", function (e) {
        console.log('bind', 
                    e.target // this is the DOM element that triggered the event
                             // the form in this case
                    e);
    });

    // Triggering can be simplified a lot
    $('form').trigger('EVENT');

});


The jQuery trigger documentation says that extra parameters should be passed in an array. So:

$(document).trigger("EVENT", [ element ])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜