
MooTools DOMEvent Methods.
new DOMEvent([event[, win]]);
$('myLink').addEvent('keydown', function(event){ // the passed event parameter is already an instance of the Event type. alert(event.key); // returns the lowercase letter pressed. alert(event.shift); // returns true if the key pressed is shift. if (event.key == 's' && event.control) alert('Document saved.'); //executes if the user presses Ctr+S. });
event.key
is only reliable with keydown
or keyup
events. See PPK.Stop an event from propagating and also executes preventDefault.
myEvent.stop();
<a id="myAnchor" href="http://google.com/">Visit Google.com</a>
$('myAnchor').addEvent('click', function(event){ event.stop(); //Prevents the browser from following the link. this.set('text', 'Where do you think you\'re going?'); //'this' is Element that fires the Event. (function(){ this.set('text', 'Instead visit the Blog.').set('href', 'http://blog.mootools.net'); }).delay(500, this); });
Cross browser method to stop the propagation of an event (this stops the event from bubbling up through the DOM).
myEvent.stopPropagation();
"#myChild" does not cover the same area as myElement. Therefore, the 'click' differs from parent and child depending on the click location:
<div id="myElement"> <div id="myChild"></div> </div>
$('myElement').addEvent('click', function(){ alert('click'); return false; //equivalent to stopPropagation. }); $('myChild').addEvent('click', function(event){ event.stopPropagation(); //prevents the event from bubbling up, and fires the parent's click event. });
Cross browser method to prevent the default action of the event.
myEvent.preventDefault();
<form> <input id="myCheckbox" type="checkbox" /> </form>
$('myCheckbox').addEvent('click', function(event){ event.preventDefault(); //prevents the checkbox from being "checked". });
This function allows to add an additional event key code.
DOMEvent.defineKey(16, 'shift'); $('myInput').addEvent('keydown', function(event){ if (event.key == 'shift') alert('You pressed shift.'); });
This function allows to add additional event key codes.
DOMEvent.defineKeys({ '16': 'shift', '17': 'control' }); $('myInput').addEvent('keydown', function(event){ if (event.key == 'control') alert('You pressed control.'); });
© Linux.ria.ua, 2008-2024 |