2 回答

TA貢獻(xiàn)1712條經(jīng)驗 獲得超3個贊
您已將此問題標(biāo)記為 PHP 和 JS,因此我強(qiáng)烈建議您在 PHP(或您可能正在使用的任何其他服務(wù)器端語言)中執(zhí)行某種常見的包含,而不是嘗試這樣做客戶端(如 smootok 所示)主要是因為它會更加可靠,但也因為它在客戶端上也會更快。
例如: <?php include("includes/header.php");?>

TA貢獻(xiàn)1942條經(jīng)驗 獲得超3個贊
你可以用你想要的內(nèi)容創(chuàng)建一個新的文件頭,然后使用w3-include-html屬性包含它
例子
<div w3-include-html="header.html"></div>
添加javascript
<script>
function includeHTML() {
var z, i, elmnt, file, xhttp;
/* Loop through a collection of all HTML elements: */
z = document.getElementsByTagName("*");
for (i = 0; i < z.length; i++) {
elmnt = z[i];
/*search for elements with a certain atrribute:*/
file = elmnt.getAttribute("w3-include-html");
if (file) {
/* Make an HTTP request using the attribute value as the file name: */
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {elmnt.innerHTML = this.responseText;}
if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
/* Remove the attribute, and call this function once more: */
elmnt.removeAttribute("w3-include-html");
includeHTML();
}
}
xhttp.open("GET", file, true);
xhttp.send();
/* Exit the function: */
return;
}
}
}
</script>
在頁面底部調(diào)用 includeHTML() :
<script>
includeHTML();
</script>
參考:https : //www.w3schools.com/howto/howto_html_include.asp
添加回答
舉報