开发者

jquery, trigger, bind, strange parameter

开发者 https://www.devze.com 2022-12-18 14:47 出处:网络
I have the following code: <body> <form> <input type=\"text\"/> </form> <script>

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

精彩评论

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

关注公众号