You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
694 B

function includeHTML() {
var elements = document.getElementsByTagName("include");
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var file = element.getAttribute("src");
if (file) {
(function(element) { // 创建一个函数作用域,以便保存每个元素的引用
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
element.innerHTML = this.responseText;
}
};
xhttp.open("GET", file, true);
xhttp.send();
})(element);
}
}
}
document.addEventListener("DOMContentLoaded", includeHTML);