JS various browser compatibility issues

Source: Internet
Author: User
Tags event listener

IE low-version browsers and other browsers are generally different, in order to achieve compatibility, you can use the following methods:/* Add event listener for elements compatible with various browsers */
if (div.attachevent) {//ie browser
Div.attachevent ("onclick", show);
}else{
Div.addeventlistener ("click", Show,false);
}
/* Same reason for element removal event listener compatible with various browsers */
if (div.detachevent) {
Div.detachevent ("onclick", show);
}else{
Div.removeeventlistener ("click", Show,false);} When a parent container (such as a Div) contains several subset containers (DIV), any click on a container (DIV) will trigger events for other containers. To solve such problems, the example is as follows://false event bubbling (from child element to parent Element) True: Event capture (from parent element to child element) Div1.addeventlistener ("click", Showconsole1,false); function ShowConsole1 (e) {
E = e | | window.event;//event bubbling processing prevents event bubbling if (e.stoppropagation) {
E.stoppropagation (); Non-IE
}else{
E.cancelbubble = true;
}

Console.log ("Div1"); } <a href= "https://www.baidu.com/" onclick= "Showbaidu" (event) > find him thousands of </a>/* three: Cancel the default event */
function Showbaidu (e) {
if (E.preventdefault) {/* Non IE browser */
E.preventdefault ();
}else{/*ie Browser */
E.returnvalue = false;
}
Console.log ("Don't jump! ");}

JS various browser compatibility issues

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.