了解 w3.includeHTML JavaScript 函数的递归
Understand recursion of w3.includeHTML JavaScript function
下面的 JavaScript 函数包括 HTML 来自带有 class "w3-include-html."
标签的 HTML 文件
有人可以解释为什么 w3.includeHTML(cb),所以函数本身,在函数中间再次被调用吗?这不会产生无限循环吗?非常感谢任何帮助。
w3.includeHTML = function(cb) {
var z, i, elmnt, file, xhttp;
z = document.getElementsByTagName("*");
for (i = 0; i < z.length; i++) {
elmnt = z[i];
file = elmnt.getAttribute("w3-include-html");
if (file) {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
elmnt.innerHTML = this.responseText;
elmnt.removeAttribute("w3-include-html");
w3.includeHTML(cb);
}
}
xhttp.open("GET", file, true);
xhttp.send();
return;
}
}
if (cb) cb();
};
Can someone explain why w3.includeHTML(cb), so the function itself, is called again in the middle of the function?
因为您包含的文件试图包含其他内容。
Doesn't that generate an infinite loop?
否,因为上一行删除了已完成包含的元素的属性。
下面的 JavaScript 函数包括 HTML 来自带有 class "w3-include-html."
标签的 HTML 文件有人可以解释为什么 w3.includeHTML(cb),所以函数本身,在函数中间再次被调用吗?这不会产生无限循环吗?非常感谢任何帮助。
w3.includeHTML = function(cb) {
var z, i, elmnt, file, xhttp;
z = document.getElementsByTagName("*");
for (i = 0; i < z.length; i++) {
elmnt = z[i];
file = elmnt.getAttribute("w3-include-html");
if (file) {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
elmnt.innerHTML = this.responseText;
elmnt.removeAttribute("w3-include-html");
w3.includeHTML(cb);
}
}
xhttp.open("GET", file, true);
xhttp.send();
return;
}
}
if (cb) cb();
};
Can someone explain why w3.includeHTML(cb), so the function itself, is called again in the middle of the function?
因为您包含的文件试图包含其他内容。
Doesn't that generate an infinite loop?
否,因为上一行删除了已完成包含的元素的属性。