Cross-browser event handlers and cross-browser events

Source: Internet
Author: User

Cross-browser event handlers and cross-browser events

Var eventUtil = {// defines an object addEventHandler: function (element, type, handler) {// handler is the trigger operation, that is, function. When passing parameters, function is not enclosed in parentheses. If (element. addEventListener) {// supports dom2-level elements. addEventListener (type, handler, false);} else if (element. attachEvent) {// supports ieelement. attachEvent ('on' + type, handler);} else {element ['on' + type] = handler; // not used here. the reason is. A variable cannot be connected to a string, in fact any. can be represented in brackets}, removeEventHander: function (element, type, hander) {if (element. removeEventListener) {// do not add parentheses to check whether it has this attribute element. removeEventListener (type, hander, false); // brackets must be added here} else if (element. detachEvent) {element. detachEvent ('on' + type, hander);} else {element ['on' + type] = null ;}}}

Event object: An event object is generated when a dom event is triggered.

Event object in dom: event

1. The type attribute is used to obtain the event type.

2. The target attribute is used to obtain the event target.

 

Event bubbling: if you trigger an event of a child element, the browser triggers the event of the parent element of the child element by default.

3. method to prevent event bubbling: event. stopPropagation (), which is placed in the handler of the child element.

4. The preventDefault () method blocks the default action of an event. For example, the default action of tag a is to jump.

Event object in ie: window. event

1. type attribute, used to obtain the Event type

2. srcElement attribute, used to obtain the event Target

3. cancelBubble is used to prevent event bubbles

4. returnValue is used to block the default action of an event.

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.