我需要重構此代碼以避免代碼重複。JavaScript重構/避免重複
$('#showmore-towns').toggle(
function() {
$('.popularTownsAdditional').show();
console.log(this);
$('#showmore-town .showless').show();
$('#showmore-town .showmore').hide();
$('#showmore-town').removeClass('sd-dark28').addClass('sd-dark28down');
return false;
},
function() {
$('.popularTownsAdditional').hide();
$('.showless').hide();
$('.showmore').show();
$('#showmore-towns').addClass('sd-dark28').removeClass('sd-dark28down');
});
$('#showmore-cities').toggle(
function() {
$('.popularCitiesAdditional').show();
$('#showmore-cities .showless').show();
$('#showmore-cities .showmore').hide();
$('#showmore-cities').removeClass('sd-dark28').addClass('sd-dark28down');
return false;
},
function() {
$('.popularCitiesAdditional').hide();
$('#showmore-cities .showless').hide();
$('#showmore-cities .showmore').show();
$('#showmore-cities').addClass('sd-dark28').removeClass('sd-dark28down');
});
基本上,它顯示了相同的功能,但只對具有不同ID的不同的div。
嘗試http://codereview.stackexchange.com – Brad 2012-07-31 04:50:12
我們展示的HTML代碼。 – 2012-07-31 04:59:41