Internet Explorer Event Object)

Source: Internet
Author: User

Let's look at the example below, Copy codeThe Code is as follows: var btn = document. getElementById ('mybtn ');
Btn. onclick = function (){
Var event = window. event;
Alert (event. type); // "click"
}

In this case, the event object comes from the window. event object and is then used to determine the event type. However, when eventHandler is authorized by attachEvent (), the event object will be the unique parameter of the function. See the following code.Copy codeThe Code is as follows: var btn = document. getElementById ("myBtn ");
Btn. attachEvent ("onclick", function (event ){
Alert (event. type); // "click"
});

When the attachEvent () method is used, the event object can also be accessed on the window object, which is the same as the dom 0 implementation method and the event object is also passed in as a parameter.
If event Handler is authorized through the HTML attribute, event is accessed as a variable, called event. For exampleCopy codeThe Code is as follows: <input type = "button" value = "Click Me" onclick = "alert (event. type)">

The IE event object also contains attributes and methods, which are related to the creation of this specific event. These attributes and methods are either directly mapped to DOM attributes and methods, or related to DOM attributes and methods. For example, attributes and methods of DOM Event objects are different from those of the origin event, but the methods are as follows:
The cancleBubble Boolean readable/writable default value is false, but can be set to true to cancel event bubbling, which is the same as the stopPropagation () method in dom.
The default value of returnValue Boolean readable/writable is true. When it is set to false, the default behavior used to cancel the event is the same as that of preventDefault () in the dom.
The target of the read-only event of the srcElement element, which is the same as the target attribute in the dom.
The type of the read-only event triggered by the string.
Because the event handler event scope is determined by the authorization method, the value of this should not always point to the event target, so event. SrcElement should be used instead. Example:
   Copy codeThe Code is as follows: btn. onclick = function (){
Alert (window. event. srcElement === this); // true
};
Btn. attachEvent ("onclick", function (event ){
Alert (event. srcElement === this); // false
});

In the first event handler, the authorization is performed at the DOM0 level, so the srcElement attribute points to this, but these two values are different in the second event handler.
The returnValue attribute is equivalent to the preventDefault () method in the DOM. It is also used to cancel the default action of an event. You need to set the returnValue attribute value to false to cancel the default action of an event, see the following example;Copy codeThe Code is as follows: var link = document. getElementById ('mylink ');
Link. onclick = function (){
Window. event. returnValue = false;
};

In this example, the returnValue attribute is used to cancel the default connection behavior. Unlike dom, there is no way to determine whether an event can be canceled or javascript disabled.
The cancleBubble attribute has the same function as stopPropagation () in none of them to prevent event bubbles. Because IE8 or earlier versions of IE do not support the event capture phase, and cancleBubble only supports the cancellation of bubbling, and stopPropagation () is the cancellation of event capture and bubbling. For example:Copy codeThe Code is as follows: var btn = document. getElementById ("myBtn ");
Btn. onclick = function (){
Alert ("Clicked ");
Window. event. cancelBubble = true;
};
Document. body. onclick = function (){
Alert ("Body clicked ");
};

By setting the cancelBubble value to true in the handler of ionclick, the event is blocked from bubbling to document. body event processing. Therefore, when btn is clicked, only one prompt dialog box is displayed, that is, "click ".

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.