简体   繁体   中英

Condense Repetitive jQuery statements

I am pretty green when it comes to javascript, and programming in general. Can someone give me some direction on how to shorten the below code block. I built the front end with consistent naming so I know it can be done, but I'm unsure how.

$('#fd_button').on('click', function(){
    $('#fd_content').fadeToggle("slow", "linear");
});
$('#fd_content #close_btn').on('click', function(){
    $('#fd_content').fadeToggle("slow", "linear");
});
$('#fl_button').on('click', function(){
    $('#fl_content').fadeToggle("slow", "linear");
});
$('#fl_content #close_btn').on('click', function(){
    $('#fl_content').fadeToggle("slow", "linear");
});
$('#el_button').on('click', function(){
    $('#el_content').fadeToggle("slow", "linear");
});
$('#el_content #close_btn').on('click', function(){
    $('#el_content').fadeToggle("slow", "linear");
});
$('#fm_button').on('click', function(){
    $('#fm_content').fadeToggle("slow", "linear");
});
$('#fm_content #close_btn').on('click', function(){
    $('#fm_content').fadeToggle("slow", "linear");
});
$('#gf_button').on('click', function(){
    $('#gf_content').fadeToggle("slow", "linear");
});
$('#gf_content #close_btn').on('click', function(){
    $('#gf_content').fadeToggle("slow", "linear");
});
$('#fg_button').on('click', function(){
    $('#fg_content').fadeToggle("slow", "linear");
});
$('#fg_content #close_btn').on('click', function(){
    $('#fg_content').fadeToggle("slow", "linear");
});
$('#en_button').on('click', function(){
    $('#en_content').fadeToggle("slow", "linear");
});
$('#en_content #close_btn').on('click', function(){
    $('#en_content').fadeToggle("slow", "linear");
});
$('#fmn_button').on('click', function(){
    $('#fmn_content').fadeToggle("slow", "linear");
});
$('#fmn_content #close_btn').on('click', function(){
    $('#fmn_content').fadeToggle("slow", "linear");
});
$('#gfn_button').on('click', function(){
    $('#gfn_content').fadeToggle("slow", "linear");
});
$('#gfn_content #close_btn').on('click', function(){
    $('#gfn_content').fadeToggle("slow", "linear");
});

Add a data-toggle attribute to all elements that should toggle some other element when clicked. This attribute's value should be a reference to the element you want to fadeToggle() :

Markup :

<div data-toggle="#fd_content">Some content</div>
<div data-toggle="#fm_content">Some more content</div>
<div data-toggle="#gfn_content">Even more content</div>

JS :

$("[data-toggle]").on("click", function() {
  var elementToToggle = $(this).data("toggle");
  $(elementToToggle).fadeToggle("slow", "linear");
});

See DEMO .

The answer is going to depend on your markup - if the markup for all these elements is similar, you might only need a single event handler. But taking the case where all of these things have radically different markup, you could do:

var selectors = [
    ['#fd_button', '#fd_content'],
    ['#fd_content #close_btn', '#fd_content'],
    // ... etc
];

selectors.forEach(function(trigger, target) {
    $(trigger).on('click', function(){
        $(target).fadeToggle("slow", "linear");
    });
});

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