JS event binding function code

Source: Internet
Author: User

Mainly solved

Browser compatibility, now compatible with IE6 7 8 ff Google (nonsense)
In IE, this points to a problem.
Go directly CodeRight! Copy codeThe Code is as follows: var bind = function (object, type, FN ){
If (object. attachevent) {// IE browser
Object. attachevent ("On" + type, (function (){
Return function (event ){
Window. event. cancelbubble = true; // stop time bubble
Object. attachevent = [FN. Apply (object)]; // ---- Here I want to talk about
}) (Object), false );
} Else if (object. addeventlistener) {// other browsers
Object. addeventlistener (type, function (event ){
Event. stoppropagation (); // stop time bubble
FN. Apply (this)


// The following is a click event for the aaa id.
BIND (document. getelementbyid ("AAA"), "click", function () {alert ("this is the ID of the button you clicked" + this. ID + "this is the bound 1st functions ")});
BIND (document. getelementbyid ("AAA"), "click", function () {alert ("this is the ID of the button you clicked" + this. ID + "this is the bound 2nd functions ")});

The code is simple and does not need to be explained. Use it to know. Haha.

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.