jQuery Ajax 重新加载 iframe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3555984/
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
Ajax Reload iframe
提问by Novice
How can I reload iframe using jquery?
如何使用 jquery 重新加载 iframe?
<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.js" > </script>
<script type="text/javascript">
//Reload Iframe Function
</script>
</head>
<body>
<iframe name="f1" id = "f1" src="http://www.google.com.pk/search?q=usa+current+time&ie=utf-8&oe=utf-8&aq=t&client=firefox-a&rlz=1R1GGLL_en" width=500px height=250px>
</iframe>
<button onClick="abc()"> Reload </button>
</body>
</html>
回答by C???
Based on your comment to Haim's post, what you want could be simplified as:
根据您对 Haim 帖子的评论,您想要的可以简化为:
$('#reload').click(function() {
$('#f1').attr('src', $('#f1').attr('src'));
return false;
});
Or
或者
$('#reload').click(function() {
$('#f1')[0].contentWindow.location.reload(true);
return false;
});
But perhaps his answers better suits your needs.
但也许他的回答更适合您的需求。
UPDATE:
更新:
I put together a working example using the method I provided above. I have an <iframe>
in one page that shows another page that prints the time.
我使用上面提供的方法整理了一个工作示例。我<iframe>
在一页中有一个显示打印时间的另一页。
The <iframe>
HTML(time.html):
在<iframe>
HTML(time.html时):
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
The time is now <span id="time"></span>.
<script type="text/javascript">
$(document).ready(function() {
$('#time').html((new Date()).toString());
});
</script>
</body>
And the parent page HTML(test.html):
和父页面 HTML(test.html):
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<iframe id="frame" src="file:///D:/Users/Cory/Desktop/time.html" width="500" height="250"></iframe>
<button id="reload">Refresh</button>
<script type="text/javascript">
$(document).ready(function() {
$('#reload').click(function() {
$('#frame').attr('src', $('#frame').attr('src'));
return false;
});
});
</script>
</body>
This works just fine for me in Firefox 3.6 and IE 7.
这在 Firefox 3.6 和 IE 7 中对我来说很好用。
回答by Haim Evgi
$("#reload").click(function() {
jQuery.each($("iframe"), function() {
$(this).attr({
src: $(this).attr("src")
});
});
return false;
});
This will loop through eachiFrame every time you click on a link with a class of reloadAds and set the source of the iFrame to itself
每次单击带有 reloadAds 类的链接并将 iFrame 的源设置为自身时,这将循环遍历每个iFrame