如果您尝试了所有其他建议,但无法让其中任何一个发挥作用(就像我做不到一样),那么您可以尝试以下可能有用的方法。
HTML
<a class="refresh-this-frame" rel="#iframe-id-0">Refresh</a>
<iframe src="" id="iframe-id-0"></iframe>
JS
$('.refresh-this-frame').click(function() {
var thisIframe = $(this).attr('rel');
var currentState = $(thisIframe).attr('src');
function removeSrc() {
$(thisIframe).attr('src', '');
}
setTimeout (removeSrc, 100);
function replaceSrc() {
$(thisIframe).attr('src', currentState);
}
setTimeout (replaceSrc, 200);
});
我最初打算尝试通过 RWD 和跨浏览器测试来节省一些时间。我想创建一个包含一堆 iframe 的快速页面,这些 iframe 被组织成我可以随意显示/隐藏的组。从逻辑上讲,您希望能够轻松快速地刷新任何给定的帧。
我应该注意,我目前正在从事的项目,也就是在这个测试平台中使用的项目,是一个带有索引位置的单页站点(例如 index.html#home)。这可能与为什么我无法获得任何其他解决方案来刷新我的特定框架有关。
话虽如此,我知道这不是世界上最干净的东西,但它适用于我的目的。希望这可以帮助某人。现在,如果我能弄清楚每次 iframe 内有动画时如何防止 iframe 滚动父页面...
编辑:
我意识到这不会像我希望的那样“刷新” iframe。它会重新加载 iframe 的初始源。仍然无法弄清楚为什么我无法使用其他任何选项..
更新:
我无法让任何其他方法起作用的原因是因为我在 Chrome 中测试它们,如果它不是源自相同的位置(据我所知)。经过进一步测试,我也无法访问 FF 中的 contentWindow。
修改后的 JS
$('.refresh-this-frame').click(function() {
var targetID = $(this).attr('rel');
var targetSrc = $(targetID).attr('src');
var cleanID = targetID.replace("#","");
var chromeTest = ( navigator.userAgent.match(/Chrome/g) ? true : false );
var FFTest = ( navigator.userAgent.match(/Firefox/g) ? true : false );
if (chromeTest == true) {
function removeSrc() {
$(targetID).attr('src', '');
}
setTimeout (removeSrc, 100);
function replaceSrc() {
$(targetID).attr('src', targetSrc);
}
setTimeout (replaceSrc, 200);
}
if (FFTest == true) {
function removeSrc() {
$(targetID).attr('src', '');
}
setTimeout (removeSrc, 100);
function replaceSrc() {
$(targetID).attr('src', targetSrc);
}
setTimeout (replaceSrc, 200);
}
if (chromeTest == false && FFTest == false) {
var targetLoc = (document.getElementById(cleanID).contentWindow.location).toString();
function removeSrc() {
$(targetID).attr('src', '');
}
setTimeout (removeSrc, 100);
function replaceSrc2() {
$(targetID).attr('src', targetLoc);
}
setTimeout (replaceSrc2, 200);
}
});