JavaScript Advanced Programming Second Edition 12th Chapter summary of Event Essentials (commonly used cross-browser detection methods) _javascript skills

Source: Internet
Author: User
Copy Code code as follows:

var eventutil={//cross-Browser Handler---creation method
Addhandler:function (Element,type,handler) {
if (Element.addeventlistener) {
Element.addeventlistneter (Type,handler,false);
}else if (element.attachevent) {
Element.attachevent ("on" +type,handler);
}else{
element["on" +type]=handler;
}
}
Removehandler:function (Element,type,handler) {//cross-Browser Handler---Delete method
if (Element.removeeventlistener) {
Element.removeeventlistneter (Type,handler,false);
}else if (element.detachevent) {
Element.detachevent ("on" +type,handler);
}else{
element["on" +type]=handler;
}
}
Getevent:function (Event) {//cross-Browser Events object---Returns a reference to an event object
return event?event:window.event;
}
Gettarget:function (Event) {//cross-Browser Events object---return event target
return event.target| | Event.srcelement;
}
Preventdefault:function (Event) {//cross-Browser Events object---Cancel default event
if (Event.preventdefault) {
Event.preventdefault ();
}else{
Event.returnvalue=false;
}
}
Stoppropagation:function (Event) {//cross-browser Event object---block flow of events
if (event.stoppropagation) {
Event.stoppropagation ();
}else{
Event.cancebubble=false;
}
}
Getrelatedtarget:function (Event) {//cross-browser to get related elements
if (event.relatedtarget) {
return event.relatedtarget;
}else if (event.toelement) {
return envent.toelement;
}else if (event.fromelement) {
return event.fromelement;
}else{
return null;
}
}
Getbutton:function (Event) {//Mouse event button Property detection
if (Document.implementation.hasFeature ("MouseEvent", "2.0")) {
return Event.button;
}else{
Switch (Event.button) {
Case 0:
Case 1:
Case 3:
Case 5:
Case 7:
return 0;
Case 2:
Case 6:
Return 2
Case 4:
Return 1
}
}
}
Getcharcode:function (Event) {//cross-browser character encoding---charcode property detection
if (typeof event,charcode== "number") {
return event.charcode;
}else{
return event.keycode;
}
}
}

Event delegates: Too many solutions for event handlers that reduce memory and improve performance;
Simulation event: This is more complex, to be studied slowly;
Related Article

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.