Прив'язує слухача події до DOM-елемента.
myElement.addEvent(type, fn);
<div id="myElement">Click me.</div>
$('myElement').addEvent('click', function(){ alert('clicked!'); });
Працює як Element.addEvent, але замість цього видаляє вказаний слухач події.
myElement.removeEvent(type, fn);
var destroy = function(){ alert('Boom: ' + this.id); } // this refers to the Element. $('myElement').addEvent('click', destroy); // later $('myElement').removeEvent('click', destroy);
var destroy = function(){ alert('Boom: ' + this.id); } var boundDestroy = destroy.bind($('anotherElement')); $('myElement').addEvent('click', boundDestroy); // later $('myElement').removeEvent('click', destroy); // this won't remove the event. $('myElement').removeEvent('click', destroy.bind($('anotherElement')); // this won't remove the event either. $('myElement').removeEvent('click', boundDestroy); // this is the correct way to remove the event.
Те саме, що Element:addEvent, але приймає об'єкт для додавання кількох подій одночасно.
myElement.addEvents(events);
$('myElement').addEvents({ 'mouseover': function(){ alert('mouseover'); }, 'click': function(){ alert('click'); } });
Видаляє всі події окремого типу з Element. Якщо аргумент не передано, видаляє всі події всіх типів.
myElements.removeEvents([events]);
var myElement = $('myElement'); myElement.addEvents({ 'mouseover': function(){ alert('mouseover'); }, 'click': function(){ alert('click'); } }); myElement.addEvent('click', function(){ alert('clicked again'); }); myElement.addEvent('click', function(){ alert('clicked and again :('); }); //addEvent will keep appending each function. //Unfortunately for the visitor, that'll be three alerts they'll have to click on. myElement.removeEvents('click'); // This saves the visitor's finger by removing every click event.
Виконує всі події вказаного типу, присутні в Element.
myElement.fireEvent(type[, args[, delay]]);
// Fires all the added 'click' events and passes the Element 'anElement' after one second. $('myElement').fireEvent('click', $('anElement'), 1000);
Клонує всі події з Element в цей Element.
myElement.cloneEvents(from[, type]);
var myElement = $('myElement'); var myClone = myElement.clone().cloneEvents(myElement); //clones the element and its events
Ви можете додати додаткові користувацькі події, додаючи властивості (об'єкти) до Хешу Element.Events
Element.Events.yourproperty (об'єкт) може мати:
Element.Events.shiftclick = { base: 'click', //we set a base type condition: function(event){ //and a function to perform additional checks. return (event.shift == true); //this means the event is free to fire } }; $('myInput').addEvent('shiftclick', function(event){ log('the user clicked the left mouse button while holding the shift key'); });
Існують різні типи користувацьких Events, які ви можете створити:
Якщо ви використовуєте опцію condition, ви ПОВИННИ вказати базовий тип, якщо не плануєте перезаписати нативну подію. (дуже не рекомендується: використовуйте лише тоді, коли точно знаєте, що робите).
Ця подія викликається, коли миша входить в область DOM-елемента і не викликається знову, якщо миша переходить через дочірні елементи (на відміну від mouseover).
$('myElement').addEvent('mouseenter', myFunction);
Ця подія викликається, коли миша залишає область DOM-елемента і не викликається, якщо миша переходить через дочірні елементи (на відміну від mouseout).
$('myElement').addEvent('mouseleave', myFunction);
Ця подія викликається при прокручуванні коліщатка миші;
$('myElement').addEvent('mousewheel', myFunction);
| © Linux.ria.com, 2008-2026 |