Consider the following HTML:
<select value="val2">
<option value="val1">o1</option>
<option value="val2">o2</option>
</select>
And JavaScript (performed on document ready):
var $select = $('select');
var select = $select.get(0);
function logger(msg) {
return function () { console.log(msg); };
}
$select.on('change', logger('jquery on select'));
$(document).on('change', logger('jquery on document'));
select.addEventListener('change', logger('native on select'), false);
document.addEventListener('change', logger('native on document'), false);
setTimeout(function () {
console.log(' == programmatic ==');
$select.trigger('change');
console.log(' == now try manual ==');
}, 1000);
This results to the following output in the console:
== programmatic ==
jquery on select
jquery on document
== now try manual ==
jquery on select
native on select
jquery on document
native on document
The question is: why are natively bound listeners not called? How make them be called?
Here's also a jsFiddle: http://jsfiddle.net/PVJcf/
(Using jQuery 2.0.2)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…