sách gpt4 ai đã đi

Làm thế nào để tải một phần HTML từ một HTML khác?

In lại 作者:搜寻专家 更新时间:2023-10-31 22:53:56 51 4
mua khóa gpt4 Nike

我正在使用 HTML 写一本书。如果我把它写在一个 html 文件中,整个代码就会变长,所以我想将每一章保存到不同的文件中,然后将它们加载到主 html 中。我的意思是有像 chapter1.html,chapter2.html 这样的文件,...我想将它们的内容包含在另一个文件 main.html 中.

Liệu điều này có thể thực hiện được không?

1 Câu trả lời

如果您希望动态执行此操作。你可以使用 javascriptCòn hơn là .将要包含的 HTML 内容保存在 content.html ở giữa


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("data-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("data-include-html");
includeHTML();
}
}
xhttp.open("GET", file, true);
xhttp.send();
/*exit the function:*/
return;
}
}
}

您可以稍后将其添加到 HTML 页面,如下所示。


关于html - 如何从另一个 HTML 加载部分 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540539/

51 4 0
Bài viết được đề xuất: php - 将一个数字分成随机不相等的部分
Bài viết được đề xuất: php - Sublimelinter - 如何在错误时禁用窗口弹出窗口
Bài viết được đề xuất: php - 使用 URL 参数获取当前文件名
Bài viết được đề xuất: php - 如何删除 laravel 5 中的帖子资源?