简体   繁体   中英

Combine 2 jQuery functions shorthand?

I have this:

$("#about-us .faq-cta").click(function () {
    $('#faq').load('faqs.html #main').delay(100).slideToggle('1000', "easeOutQuad", function () {
        $.waypoints('refresh');
        $("#siteNav li a").removeClass("siteNavSelected");
    }, {
        offset: function () {
            return $.waypoints('viewportHeight') - $(this).outerHeight();
        }
    });
});

And this:

if (window.location.hash.toLowerCase() === "#faq") {
    $("#about-us .faqCta").click(function () {
        $('#faq').load('faqs.html #main').delay(100).slideToggle('1000', "easeOutQuad", function () {
            $.waypoints('refresh');
            $("#siteNav li a").removeClass("siteNavSelected");
        }, {
            offset: function () {
                return $.waypoints('viewportHeight') - $(this).outerHeight();
            }
        });
    });
}

Each do exactly the same thing.

Is there a way to write a bit of shorthand to combine the two?

Use:

var fcOnClick = function () {
    $('#faq').load('faqs.html #main').delay(100).slideToggle('1000', "easeOutQuad", function () {
        $.waypoints('refresh');
        $("#siteNav li a").removeClass("siteNavSelected");
    }, {
        offset: function () {
            return $.waypoints('viewportHeight') - $(this).outerHeight();
        }
    });
};

var $cont= $('#about-us');
$('.faq-cta',$cont).click(fcOnClick);
if(window.location.hash.toLowerCase() === "#faq"){
    $('.faqCta',$cont).click(fcOnClick);
}

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