请参阅以下内容:
$('body').on('whyhellothere', function(){
console.log('done...');
});
$('body').triggerHandler('whyhellothere');
此片段返回:
done...
如果我们颠倒顺序:
$('body').triggerHandler('whyhellothere');
$('body').on('whyhellothere', function(){
console.log('done...');
});
该片段不返回任何内容。为什么会这样呢?
最佳答案
如果你在森林里大喊,然后我就过来了,我什么也听不到,不是吗?
您在事件触发后注册事件处理程序。注册的处理程序只能监听开始监听后触发的事件。
这是简单的物理原理:P
关于javascript - jQuery 事件和触发器的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14653771/