iT邦幫忙

2024 iThome 鐵人賽

DAY 7
0
JavaScript

「前端開發快速入門:從基礎到現代框架的實戰之旅」系列 第 7

【Day 7】前端開發之旅-代碼優化

  • 分享至 

  • xImage
  •  

今天讓我們來稍微把程式碼改為稍作優化和修改,修改的部分主要有以下兩點:

  1. document.getElementById() 改為使用更彈性的 document.querySelector() ,讓後續維護和修改時只需要針對 CSS Selector 去更改就好。
  2. Function 改為 Arrow Function,採用更簡潔且現代流行的寫法。

原來 index.js 內的程式碼:

const modal = document.getElementById("projectModal");
const modalTitle = document.getElementById("modalTitle");
const modalDescription = document.getElementById("modalDescription");
const modalTags = document.getElementById("modalTags");
const modalCategories = document.getElementById("modalCategories");

function showProjectDetails(projectId) {
    const projectDetails = getProjectDetails(projectId);

    modalTitle.textContent = projectDetails.title;
    modalDescription.textContent = projectDetails.description;
    modalTags.textContent = projectDetails.tags;
    modalCategories.textContent = projectDetails.categories;

    modal.style.display = "block";
}

function closeProjectDetails() {
    modal.style.display = "none";
}

function getProjectDetails(projectId) {
    const projects = {
        1: {
            title: "Project 1",
            description: "詳細描述 Project 1...",
            tags: ["HTML", "CSS", "JavaScript"],
            categories: ["Web Development"]
        },
        2: {
            title: "Project 2",
            description: "詳細描述 Project 2...",
            tags: ["Vue", "React", "Angular"],
            categories: ["Frontend"]
        },
        3: {
            title: "Project 3",
            description: "詳細描述 Project 3...",
            tags: ["Node.js", "Golang", "Python"],
            categories: ["Backend"]
        }
    };
    return projects[projectId];
}

window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

修改後的 index.js 程式碼:

const modal = document.querySelector("#projectModal");
const modalTitle = document.querySelector("#modalTitle");
const modalDescription = document.querySelector("#modalDescription");
const modalTags = document.querySelector("#modalTags");
const modalCategories = document.querySelector("#modalCategories");
 
const showProjectDetails = (projectId) => {
    const projectDetails = getProjectDetails(projectId);

    modalTitle.textContent = projectDetails.title;
    modalDescription.textContent = projectDetails.description;
    modalTags.textContent = projectDetails.tags;
    modalCategories.textContent = projectDetails.categories;

    modal.style.display = "block";
}

const closeProjectDetails = () => {
    modal.style.display = "none";
}

const getProjectDetails = (projectId) => {
    const projects = {
        1: {
            title: "Project 1",
            description: "詳細描述 Project 1...",
            tags: ["HTML", "CSS", "JavaScript"],
            categories: ["Web Development"]
        },
        2: {
            title: "Project 2",
            description: "詳細描述 Project 2...",
            tags: ["Vue", "React", "Angular"],
            categories: ["Frontend"]
        },
        3: {
            title: "Project 3",
            description: "詳細描述 Project 3...",
            tags: ["Node.js", "Golang", "Python"],
            categories: ["Backend"]
        }
    };
    return projects[projectId];
}

window.onclick = (event) => {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

上一篇
【Day 6】前端開發之旅-代碼分離
系列文
「前端開發快速入門:從基礎到現代框架的實戰之旅」7
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言