Pass object to jQuery.on()

I read somewhere that it is possible to do something like this:

$(document).on({click: function(){}})

Is this true or is there something similar to this that jQuery supports? I would like to pass an object with settings.


Yes, it is possible. It is an easy way to bind more than one event at once, like this:

$(target).on({
    click: function() { /*... do something when user clicks ...*/ },
    mouseover: function() { /*... do something else ...*/ },
    keyup: function(){ /* ... */ }
    // and so on...
});

You can even do this to bind a function to multiple events:

$(target).on('click mouseover keyup', function() { 
    // something to handle all events... 
});

See .on() documentation on jQuery

链接地址: http://www.djcxy.com/p/96926.html

上一篇: 在jQuery中处理多个事件

下一篇: 将对象传递给jQuery.on()