【发布时间】:2017-10-24 16:54:32
【问题描述】:
假设,我有一个这样的文本文件:
<button onclick="logIn.newTask();">Log In</button>
现在,在 Javascript 中,我想用一个简单的 document.appendChild(); 将其加载到我的 HTML 页面中,如下所示:
var client = new XMLHttpRequest();
client.open('GET', 'http://localhost/button.txt');
client.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.appendChild(client.responseText); // Load the button see above.
}
}
client.send();
我的问题是这段代码给了我这个错误:
所以,我试图找到如何将文本转换为节点,但我在这个主题中找不到任何东西。
小问题:
我想在我的网页中加载一些 html 的元素,但我找不到任何方法来实现这个目的。
非常感谢任何帮助。
如果您有任何问题或 cmets,请告诉我。
【问题讨论】:
-
您实际上并不想将其直接附加到
document之外的</body>。
标签: javascript html nodes appendchild