2 回答

TA貢獻1859條經驗 獲得超6個贊
添加帶有 id 的 div 并更改它innerHTML似乎有效,可能會“自殺”,因為像 @MarkusZeller 所說的那樣,腳本位于體內。下面的代碼有效:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JUST_LOAD</title>
<!-- <script defer src="/index.js"></script> -->
</head>
<body>
PLEASSDETY FGUIRWFVUI
<main id="content"></main>
<script>
function loadContent() {
fetch("/content.html")
.then((res) => res.text())
.then((text) => (document.getElementById("content").innerHTML = text));
}
loadContent();
</script>
</body>
</html>

TA貢獻1854條經驗 獲得超8個贊
或者把它放在頭部
window.addEventListener("load",() => {
fetch("/content.html")
.then(res => res.text())
.then(text => document.body.innerHTML = text);
})
添加回答
舉報