jQuery 'input' event

后端 未结 8 765
执念已碎
执念已碎 2020-11-22 08:53

I\'ve never heard of an event in jQuery called input till I saw this jsfiddle.

Do you know why it\'s working? Is it an alias for keyup or s

8条回答
  •  半阙折子戏
    2020-11-22 09:09

    Using jQuery, the following are identical in effect:

    $('a').click(function(){ doSomething(); });
    $('a').on('click', function(){ doSomething(); });
    

    With the input event, however, only the second pattern seems to work in the browsers I've tested.

    Thus, you'd expect this to work, but it DOES NOT (at least currently):

    $(':text').input(function(){ doSomething(); });
    

    Again, if you wanted to leverage event delegation (e.g. to set up the event on the #container before your input.text is added to the DOM), this should come to mind:

    $('#container').on('input', ':text', function(){ doSomething(); });
    

    Sadly, again, it DOES NOT work currently!

    Only this pattern works:

    $(':text').on('input', function(){ doSomething(); });
    

    EDITED WITH MORE CURRENT INFORMATION

    I can certainly confirm that this pattern:

    $('#container').on('input', ':text', function(){ doSomething(); });
    

    NOW WORKS also, in all 'standard' browsers.

提交回复
热议问题