Cross-browser event handling

Source: Internet
Author: User

In the Web, the DOM event section mentions how to implement event handling in Chrome,ie, and a lower version of the browser, and here's a summary:

1. Non-IE browsers support AddEventListener and RemoveEventListener.

2.IE browsers (low version) support Attachevent and detachevent, but other browsers are not supported.

3. Each browser (regardless of the lower version) supports DOM0-level events, but one drawback is that it cannot be processed repeatedly.

So we're going to have to do the cross-browser processing, we need to use the handle operation, here in Click for example, look at the code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

A handle AddHandler is encapsulated in the variable eventutil, which is used to pass the event to be implemented, the required parameter is element, type (event, here is the click action), Handler (response function)

Then make an If judgment:

If the execution of AddEventListener is performed directly, the first two parameters of the AddEventListener are the events just passed in and their responses, and the last false indicates bubbling with events;

If you cannot perform the above, but can execute attachevent, ie, then call attachevent, note that the type in the parameter needs to be preceded by "on", because we passed to the handle is omitted in the But in the attachevent parameter on can not be omitted, such as Click to be added to the onclick;

The last compatibility process is direct element.onclick, where the brackets are used instead of the dots, and also to match the add on. Element. " On "+type This syntax is wrong, so use the brackets instead.

Click the button with a different browser to pop out of the window.


In the same way, we can add a delete handle, and then click the button to not react:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


Cross-browser event handling

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.