function createContent() {
const mainContent = document.querySelector("#content");
return mainContent;
}
function renderSite() {
createContent();
mainContent.appendChild(createHeader());
mainContent.appendChild(createMain());
}
每當我運行 renderSite 時,都會收到一條錯誤訊息,告訴我 mainContent 未定義。我可以通過手動將 mainContent 添加到 renderSite 來解決此問題。但是為什么從 renderSite() 呼叫 createContent() 時無法回傳 mainContent?
uj5u.com熱心網友回復:
mainContent是在createContent()函式范圍內。如果您想以這種方式使用它,您不僅需要運行它,還需要像這樣分配函式執行的結果:
function renderSite() {
const mainContent = createContent();
mainContent.appendChild(createHeader());
mainContent.appendChild(createMain());
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/413748.html
標籤:
