JavaScript-compatible event _javascript tips for individual browsers

Source: Internet
Author: User

Invoke Event:

Event Object
What is an event object? Events that trigger the DOM will generate an event object. For example, when the mouse clicks, they will produce such as the type of click Ah, and the element issued by the
The DOM event object Type property is used to get the event object, the target property is used to get the event target, and the Stoppropagation () method blocks event bubbling Preventdefault The default behavior of blocking events
The Event object Type property in IE is used to get the event object, and the Srcelement property is used to get the event target cancelbubble property to prevent event bubbling set to true to indicate that blocking false means not to block
The ReturnValue property is used to block when the default behavior of an event is set to False

Copy Code code as follows:

Compatible across browser event handling-------Unified Encapsulation
var eventhandle = {
Element: Elements, type: Click event, handle: Method of implementation
Add handle
Addeventhandle:function (element, type, handler) {
if (Element.addeventlistener) {
Element.addeventlistener (type, handler, false);//---false///represents bubbling dom2 level
}
else if (element.attachevent) {
Element.attachevent ("On" +type, Handler);
} else {
Element["on" + type] = handler;
}
},
Removing handle event handling does not work
Removeeventhandle:function (element, type, handler) {
if (Element.removeeventlistener) {//Support DOM2 level event handling type is onclick
Element.removeeventlistener (type, handler, false); ---false//represents bubbling
}
else if (element.detachevent) {
Element.detachevent ("On" +type, Handler); Support IE
} else {
Element["on" + type] = null;//dom0 level Event handling traditional Click events
}
},
Get all objects of the event object compatible browser
Getevent:function (event) {
Return event?  Event:window.event; Window.event is required in the lower version of IE browser
},
Get event type is clicked or mouse move
Gettype:function (event) {
return event.type;
},
Get the current element
Getelement:function (event) {
return Event.target | | Event.srcelement;
},
To block the default behavior of an event
: function (Event) {
if (Event.preventdefault) {
Event.preventdefault ();
}
else {
Event.returnvalue = false;
}
},
Block event bubbling
Stoppropagation:function (event) {
if (event.stoppropagation) {
Event.stoppropagation ();
}
else {
Event.cancelbubble = true;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.