仅使用 javascript 而不使用 jquery 重新加载 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16987135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reload div with only javascript without jquery
提问by user2463374
With jQuery I can update the contents of a div I using $.load()
. How can I update a div in pure JavaScript?
使用 jQuery,我可以更新我使用的 div 的内容$.load()
。如何在纯 JavaScript 中更新 div?
采纳答案by Jacopofar
As suggested by Julian H. Lam:
正如 Julian H. Lam 所建议的:
var req = new XMLHttpRequest();
req.open("POST", "address_for_div_content", true);
req.onreadystatechange = function () {
if (req.readyState != 4 || req.status != 200) return;
document.getElementById('id_of_div')=req.responseText;
};
Herethe documentation for XMLHttpRequest
这里是 XMLHttpRequest 的文档
回答by Supplement
Pure JavaScript (not JQuery) solution : wrap your div in an iframe, give it an id myFrame, then refresh it from the child like this:
纯 JavaScript(不是 JQuery)解决方案:将您的 div 包装在 iframe 中,给它一个 id myFrame,然后像这样从 child 刷新它:
parent.document.getElementById("myFrame").reload();