Javascript 重新加载 html 元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5541854/
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 html element
提问by Pit Digger
I have a page which opens another page using window.open and does some work and refreshes whole parent page . Is it possible to refresh content of a div/table on parent page using JQuery/Javascript ?
我有一个页面,它使用 window.open 打开另一个页面并执行一些工作并刷新整个父页面。是否可以使用 JQuery/Javascript 在父页面上刷新 div/table 的内容?
FYI : Here the content of the div is not changing there is an image inside div which is edited by child window which I want to update but that image does not have unique id so I want to refresh whole div .
仅供参考:这里 div 的内容没有改变,div 内有一个图像由我想更新的子窗口编辑,但该图像没有唯一的 id,所以我想刷新整个 div。
Thanks.
谢谢。
采纳答案by JohnP
You can use the .load()
method of jQuery to quickly update the contents of a container
可以使用.load()
jQuery的方法快速更新容器的内容
Simple as
简单如
$('#result').load('ajax/test.html', function() {
alert('Load was performed.');
});
回答by Jim Blackler
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
,然后像这样从子级刷新它:
parent.document.getElementById("myFrame").reload();
回答by Ortiga
$("#myDiv", window.parent.document).load("page.html", function(){
alert("Portion of page loaded");
});
The second parameter of $() is the context to search into. Default to document.
$() 的第二个参数是要搜索的上下文。默认为文档。
You can use .html() instead of .load() if you want reload from html string instead of using another page.
如果您想从 html 字符串重新加载而不是使用另一个页面,则可以使用 .html() 而不是 .load() 。