JavaScript Add IE event handler

Source: Internet
Author: User

There are a lot of incompatibilities between IE and other mainstream browsers, and there are some other browser-supported methods that can't be perfectly supported in IE, first of all, which support?

1, directly in the HTML code element attribute position is the event binding handler;

2. Node.onclick=function () {} using JavaScript

IE8 and previous versions do not support AddEventListener and RemoveEventListener, supported by Attachevent () and DetachEvent ()

Both attachevent () and detachevent () require two parameters:

The first parameter is the event to bind (onclick, onmouseover ...). Note differs from AddEventListener)

The second parameter is the handler to bind (anonymous function or function name, note that if it is not an anonymous function, the function name does not have parentheses)

Before using AddEventListener, you should first detect whether the current browser supports this method, and you can encapsulate the code in an object:

<button id= "btn" > button </button><script>var eventutil = {//type Incoming click MouseOver without passing onclick, Mouseoveraddevent:function (element, type, func) {if (Element.addeventlistener) {//All major browsers except IE 8 and earlier IE version element . AddEventListener (Type, func, false);} else if (element.attachevent) {//IE 8 and earlier IE version element.attachevent (' On ' +type, func);} else {var type = "On" +type;//e Lement.type = func; Wrong, will not succeed Element[type] = Func;}},removeevent:function (element, type, func) {if (Element.removeeventlistener) {//All Mainstream browser, except IE 8 and earlier IE version element.removeeventlistener (type, func, false);} else if (element.detachevent) {//IE 8 and earlier IE version element.detachevent (' On ' +type, func);} else {var type = "On" +type;//e Lement.type = null; Wrong, will not succeed element[type] = null;}} var btn = document.getElementById ("btn"), function Showone () {alert ("one");} Eventutil.addevent (BTN, "click", Showone); Eventutil.removeevent (BTN, "click", Showone);</script>

  

  

JavaScript Add IE event handler

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.