如何縮短以下代碼?我正處於我的旅程中。 請忽略此介紹文本的其餘部分,這只是使我的帖子更長的幾句話,因爲stackoverflow幾乎沒有我的解釋和多少代碼ist。不理會,讓你的手髒與代碼...如何優化這個冗餘代碼?
function showAllJobs() {
document.getElementById("section-01").classList.remove("hide-section");
document.getElementById("section-02").classList.remove("hide-section");
document.getElementById("section-03").classList.remove("hide-section");
document.getElementById("section-04").classList.remove("hide-section");
document.getElementById("section-05").classList.remove("hide-section");
document.getElementById("section-06").classList.remove("hide-section");
document.getElementById("section-07").classList.remove("hide-section");
}
function showJob_01() {
document.getElementById("section-01").classList.remove("hide-section");
document.getElementById("section-02").classList.add("hide-section");
document.getElementById("section-03").classList.add("hide-section");
document.getElementById("section-04").classList.add("hide-section");
document.getElementById("section-05").classList.add("hide-section");
document.getElementById("section-06").classList.add("hide-section");
document.getElementById("section-07").classList.add("hide-section");
}
function showJob_02() {
document.getElementById("section-01").classList.add("hide-section");
document.getElementById("section-02").classList.remove("hide-section");
document.getElementById("section-03").classList.add("hide-section");
document.getElementById("section-04").classList.add("hide-section");
document.getElementById("section-05").classList.add("hide-section");
document.getElementById("section-06").classList.add("hide-section");
document.getElementById("section-07").classList.add("hide-section");
}
function showJob_03() {
document.getElementById("section-01").classList.add("hide-section");
document.getElementById("section-02").classList.add("hide-section");
document.getElementById("section-03").classList.remove("hide-section");
document.getElementById("section-04").classList.add("hide-section");
document.getElementById("section-05").classList.add("hide-section");
document.getElementById("section-06").classList.add("hide-section");
document.getElementById("section-07").classList.add("hide-section");
}
function showJob_04() {
document.getElementById("section-01").classList.add("hide-section");
document.getElementById("section-02").classList.add("hide-section");
document.getElementById("section-03").classList.add("hide-section");
document.getElementById("section-04").classList.remove("hide-section");
document.getElementById("section-05").classList.add("hide-section");
document.getElementById("section-06").classList.add("hide-section");
document.getElementById("section-07").classList.add("hide-section");
}
function showJob_05() {
document.getElementById("section-01").classList.add("hide-section");
document.getElementById("section-02").classList.add("hide-section");
document.getElementById("section-03").classList.add("hide-section");
document.getElementById("section-04").classList.add("hide-section");
document.getElementById("section-05").classList.remove("hide-section");
document.getElementById("section-06").classList.add("hide-section");
document.getElementById("section-07").classList.add("hide-section");
}
function showJob_06() {
document.getElementById("section-01").classList.add("hide-section");
document.getElementById("section-02").classList.add("hide-section");
document.getElementById("section-03").classList.add("hide-section");
document.getElementById("section-04").classList.add("hide-section");
document.getElementById("section-05").classList.add("hide-section");
document.getElementById("section-06").classList.remove("hide-section");
document.getElementById("section-07").classList.add("hide-section");
}
function showJob_07() {
document.getElementById("section-01").classList.add("hide-section");
document.getElementById("section-02").classList.add("hide-section");
document.getElementById("section-03").classList.add("hide-section");
document.getElementById("section-04").classList.add("hide-section");
document.getElementById("section-05").classList.add("hide-section");
document.getElementById("section-06").classList.add("hide-section");
document.getElementById("section-07").classList.remove("hide-section");
}
我想你應該要求它在代碼審查 –
你可以代替寫一個函數,它會基於什麼論據刪除/添加(<部分的ID>)你給。 –
請將其移動代碼審查。 – murli2308