我試圖減少一些代碼,基本上下面的點擊事件被使用了10次,id爲#topNavA - #topNavJ,事件只有2個部分發生了變化。任何人都可以建議如何爲每次點擊回收此代碼?我已經添加了將從點擊事件更改爲點擊事件的行的評論,但其餘部分保持不變?所有建議歡迎。試圖縮小對多個ID使用多次的點擊事件?
$('#topNavA').click(function() {
$footerPush.hide();
$allMultipleElements.hide();
$('#section, #placement, .mobileControls').show(); // This line will change with different selectors
$('.mobileControls h1').replaceWith('<h1>Mobilizing Mobile, AL</h1>'); // This line will change with a different header 1
$('.mobileControls h2').replaceWith('<h2>Overview</h2>'); // This line will change with a different header 2
$('.menuTitle').removeClass('active');
$('.lightbox').remove();
$('#nav li > a').removeClass('active');
$('#nav li span a').css({
'color': '#8F8F8F'
});
$('#topNav').addClass('active');
$(this).css({
'color': '#e8af20'
});
});
感謝 凱爾
嗨詹姆斯,聽起來很有前途,1個問題我怎麼能每次更新標題?有沒有一個例子可以讓我看到?我還沒有真正使用數據屬性 – styler
你可以爲每個需要的東西添加額外的數據屬性,看起來像你有三個。用於不同目的的多個屬性的示例:http://jsfiddle.net/mPQfJ/2/ –
真棒詹姆斯歡呼! – styler