JavaScript Event Learning Addendum: addEventSimple

Source: Internet
Author: User

Here is an addEventSimple and removeEventSimple function. Sometimes I use these two functions to register an event handler that is less conspicuous.

Function addEventSimple (obj, evt, fn ){
If (obj. addEventListener)
Obj. addEventListener (evt, fn, false );
Else if (obj. attachEvent)
Obj. attachEvent ('on' + evt, fn );
}

Function removeEventSimple (obj, evt, fn ){
If (obj. removeEventListener)
Obj. removeEventListener (evt, fn, false );
Else if (obj. detachEvent)
Obj. detachEvent ('on' + evt, fn );
}

The usage is as follows:

AddEventSimple (element, 'click', functionname );
AddEventSimple (document, 'load', functionname2 );

Now, if an element is clicked, functionaname is triggered. The same document also has the onload event handler functionname2.

Defect: the keyword "this" in IE is not an element you think points to the trigger event, but to the entire document.

Address: http://www.quirksmode.org/js/eventSimple.html

Article introduction:

JavaScript Event Learning Chapter 1: button Detection

Chapter 10 JavaScript Event Learning: Some replaceable Event pairs

Chapter 9 JavaScript Event Learning: mouse events

JavaScript Event Learning Chapter 8: Event Sequence

JavaScript Event Learning Chapter 7: Event attributes

JavaScript Event Learning Chapter 6: Event access

JavaScript Event Learning Chapter 5: Advanced Event registration Model

JavaScript Event Learning Chapter 4: traditional Event registration Model

JavaScript Event Learning Chapter 3: early Event handlers

JavaScript Event Learning Chapter 2: Event

JavaScript Event Learning Chapter 1: Event Introduction

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.