Javascript 单击可重新加载多个iframe
我遇到了一个问题,多年来一直在寻找答案。 我有一些iFrame,里面有不同的HTML5横幅。但是它们都是同时启动的,所以我需要在循环通过选项卡时重新加载iframe HTML看起来像:Javascript 单击可重新加载多个iframe,javascript,jquery,html,css,iframe,Javascript,Jquery,Html,Css,Iframe,我遇到了一个问题,多年来一直在寻找答案。 我有一些iFrame,里面有不同的HTML5横幅。但是它们都是同时启动的,所以我需要在循环通过选项卡时重新加载iframe HTML看起来像: <div class="main" > <input type="radio" id="tab-1" name="show" checked/> <input type="radio" id="tab-2" name="show" /> <input type
<div class="main" >
<input type="radio" id="tab-1" name="show" checked/>
<input type="radio" id="tab-2" name="show" />
<input type="radio" id="tab-3" name="show" />
<input type="radio" id="tab-4" name="show" />
<input type="radio" id="tab-5" name="show" />
<input type="radio" id="tab-6" name="show" />
<input type="radio" id="tab-7" name="show" />
<div class="tab">
<label for="tab-1">320x320</label>
<label for="tab-2">300x250</label>
<label for="tab-3">300x600</label>
<label for="tab-4">320x160</label>
<label for="tab-5">160x600</label>
<label for="tab-6">930x600</label>
<label for="tab-7">930x180</label>
</div>
<div class="content">
<div class="content-dis">
<div>
<a href="http://n" target="_blank" class="the-click"></a>
<iframe id="iframe" src="https://b" width="320px" height="320px" frameborder="0" scrolling="no"></iframe>
</a>
</div>
</div>
<div class="content-dis">
<div >
<a href="http://n" target="_blank" class="the-click"></a>
<iframe id="iframe" src="https://b" width="300px" height="250px" frameborder="0" scrolling="no"></iframe>
</div>
</div>
320x320
300x250
300x600
320x160
160x600
930x600
930x180
*编辑需要注意的是,iframe内容位于不同的域上。如果动态地将src添加到iframe中,它应该在获得后立即加载 因此,例如,如果在单击选项卡时添加iFrame的src,那么它应该只在该点加载 您可以使用一个数组,其中键是iframe的ID,值是url值,也可以将url作为元标记添加到html本身中,然后在javascript代码中使用它填充iframe的src标记 下面是一个简单的示例,向您展示基本思想:
$('#test').on('click', function (){
$("iframe").each(function() {
var attr;
attr = $(this).attr('data-frame-src');
if (typeof attr !== typeof void 0 && attr !== false) {
$(this).attr('src', attr);
}
});
});
单击按钮时,框架的src将加载
目前,我正在将src添加到所有iframe中,但是您可以轻松地使用frame id作为参数运行该函数,并且只添加iframe srcurl是否位于其他域?@user1408786是的,谢谢!你能举个例子吗?是的,检查我的更新回答你确定这可以跨域工作吗?如果域在iframe中工作(它是源站允许的),它应该可以工作。您所做的基本上就是获取src并在以后添加它,而不是其他。如果你试图把谷歌放在框架上,你不能,因为这是不允许的来源(谷歌),但如果你使用的是一个允许的域名,它将完全工作。iFrame的内容没有显示在使用'data frame src'尚未,我已试着上传源到同一服务器,它仍然不工作