Summary of differences between JavaScript events in IE and Dom browsers

Source: Internet
Author: User

1. event processing functions

In ie:

Each element and a Windows Object have two methods: attachevent () and detachevent (). The first method is to add an event handler and the second method is to remove it.
1 [object]. attachevent ("name_of_event_handler", "function_to_attach ")
2 [object]. detachevent ("name_of_event_handler", "function_to_remove ")

In this way, you can add multiple event handler functions.

In the DOM Browser:
Addeventlistener () and removeeventlistener () are used to allocate and remove event handlers. It has three parameters: event name, the function to be allocated, whether the processing function is used for the bubble stage or the capture stage (the capture stage is true ).
1 [object]. addeventlistener ("name_of_event", fnhander, bcapture)
2 [object]. removeeventlistener ("name_of_event", fnhander, bcapture)

You can also add multiple event processing functions.

2. Locating event objects

In ie:
The event object is an attribute event of a Windows Object. The event handler function must access the event object as follows:
1 odiv. onclick = function (){
2 var oevent = Window. event;
3
4}

In DOM:
In the DOM standard, the event object must be passed to the event handler as a unique parameter, so do this:
1 odiv. onclick = function (){
2 var oevent = argunments [0]
3}

Or
1 odiv. onclick = function (oevent ){
2
3}

Therefore, you must use different methods to obtain the event object.
So we often see this writing method.
1 E = event | window. Event

When a logic or operator acts on two objects, the first object is not empty, and the first object is returned. Otherwise, the second object is returned.

In addition, in the arguments attribute, the event object is always the first parameter of the event processing function. That is to say, you can access the agreements [0] of the event processing function to obtain the event object.
3. Obtain the target

In ie:
The IE target is included in the srcelement attribute of the event object:
1 var otarget = oevent. srcelement;

In DOM:
The target is included in the target attribute:
1 var otarget = oevent.tar get;

IE targets can only be elements, documents, or windows. Dom-compatible browsers also allow text nodes as targets.
4. Get charactersCode

Both IE and Dom support the keycode attribute of the event object. It returns the numeric code that presses the key.

In ie:
1 var ocharcode = oevent. keycode;

In DOM:
1 var ocharcode = oevent. charcode;
5. prevent default Behaviors

In ie:
1 oevent. returnvalue = false;

In DOM:
1 oevent. preventdefault ();
6. stop bubbling.

In ie:
1 oevent. cancelbubble = true;

In DOM:
1 oevent. stoppropagation ();
7. Obtain the event Source

In ie:
1 oevent.tar get

In DOM:
1 oevent. srcelement

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.