需要帮助来缩短这个jQuery函数

时间:2016-12-14 11:20:00

标签: javascript jquery html

这是我的代码,效果很好here

在做这样的事情时需要帮助:

$('.date-container input').click(function() {
    var j;
    for (j = 1; j <= 4; j++) {

        $('.day' + (j + 1) + '-times').hide('slow');

        $(this).$('.day' + (j + 1) + '-times').show('slow');
    }
});

1 个答案:

答案 0 :(得分:3)

这是你的小提琴JS代码的简短版本:

$(document).ready(function() {
  // When document is ready, we bind the click event on every 'input' in the '.date-container' div 
  $('.date-container input').click(function(){
    // When a click event is triggered on one of those 'input', we hide
    // all the 'div' that are in the '#checkboxradio' element 
    $('#checkboxradio > div').hide('slow');
    // Using the 'id' attribute of the input that triggered the clicked
    // event (e.g. "day1"), we build a jQuery selector that'll be used
    // to select the proper checkboxes time container
    // (e.g. "." + "day1" + "-times" = ".day1-times")
    // Then we stop all animations on that element (prevent hiding if 
    // it's the element we actually want to display)
    // and trigger a show animation on that same element
    $('.' + $(this).prop('id') + '-times').stop().show('slow');
  });
});

您可能希望从复选框切换到无线电