简体   繁体   中英

How do I make this javascript reusable?

I have some code that I use many times. Here's the code and an example use:

$('.close-cookie-notice').click(function (e) {

    // make this reusable
    if (e.preventDefault) {
        e.preventDefault();
    }
    else {
        e.returnValue = false;
    }
    // end of reusable code
    $('#site-cookie-notice').slideUp();
});

The code I need to reuse is between the comments. I would like this to reside in its own function but am not sure how to deal with the passing of the event (e).

Anyone help?

function myspecialfunction(e) {
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}

Used as:

$('.close-cookie-notice').click(function (e) {
    myspecialfunction(e);
    $('#site-cookie-notice').slideUp();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM