mootools
Events are a huge part of JavaScript because so much of our JavaScript is triggered by the user's actions. Quite frequently we will assign a listener to a link and immediately want the default click action stopped. For example:
事件是JavaScript的重要组成部分,因为我们的大部分JavaScript是由用户的操作触发的。 我们经常会为链接分配一个侦听器,并立即希望停止默认的单击操作。 例如:
$('myLink').addEvent('click',function(e) {
e.stop();
//do stuff
});
The above code works great but there's one small hole I sometimes run into. On rare occasion I feel the need to trigger an event on an element:
上面的代码很好用,但有时会遇到一个小漏洞。 在极少数情况下,我感到有必要在元素上触发事件:
$('myLink').fireEvent('click');
The problem is that the Event object is not given when an event is triggered using Element.fireEvent. The way to prevent any problems is an easy if check:
问题是使用Element.fireEvent触发事件时未提供Event对象。 如果进行检查,防止任何问题的方法很容易:
$('myLink').addEvent('click',function(e) {
if(e) e.stop();
//do stuff
});
Voila. The safe way to stop events!
瞧 停止事件的安全方法!
mootools