on()è un tentativo di unire la maggior parte delle funzioni di associazione di eventi di jQuery in una. Questo ha il vantaggio di mettere in ordine le inefficienze con livevs delegate. Nelle versioni future di jQuery, questi metodi verranno rimossi e solo one onerimarranno.
Esempi:
// Using live()
$(".mySelector").live("click", fn);
// Equivalent `on` (there isn't an exact equivalent, but with good reason)
$(document).on("click", ".mySelector", fn);
// Using bind()
$(".mySelector").bind("click", fn);
// Equivalent `on`
$(".mySelector").on("click", fn);
// Using delegate()
$(document.body).delegate(".mySelector", "click", fn);
// Equivalent `on`
$(document.body).on("click", ".mySelector", fn);
Internamente, jQuery associa tutti questi metodi e setter del gestore di eventi abbreviati al on()metodo, indicando ulteriormente che dovresti ignorare questi metodi da ora in poi e usare solo on:
bind: function( types, data, fn ) {
return this.on( types, null, data, fn );
},
live: function( types, data, fn ) {
jQuery( this.context ).on( types, this.selector, data, fn );
return this;
},
delegate: function( selector, types, data, fn ) {
return this.on( types, selector, data, fn );
},
Vedi https://github.com/jquery/jquery/blob/1.7/src/event.js#L965 .