仅使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:41:21  来源:igfitidea点击:

Reload div with only javascript without jquery

javascript

提问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();