程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了同步 iframe大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决同步 iframe?

开发过程中遇到同步 iframe的问题如何解决?下面主要结合日常开发的经验,给出你关于同步 iframe的解决方法建议,希望对你解决同步 iframe有所启发或帮助;

我想同步多个 iframe。 IE。如果我在一个 iframe 中进行更改,则结果应显示在所有其他框架中。

背景: 我想在不同的显示尺寸上并行显示显示。

但是,我现在有一个循环调用。每个操作都会更改当前页面的 URL。我想在其他 iframe 中显示这个 URL。

HTML 代码

<div>
    <h3>local display landscape</h3>
    <iframe ID="iframe_unit_landscape" name="iframe_unit_landscape" onload="loadiframe(0)"
        src="http://localhost:3000/mainmenu.HTML" height="240" wIDth="320" class="iframe">
    </iframe>
</div>

<div>
    <h3>local display porTrait</h3>
    <iframe ID="iframe_unit_porTrait" name="iframe_unit_porTrait" onload="loadiframe(1)"
        src="http://localhost:3000/mainmenu.HTML" height="320" wIDth="240" class="iframe">
    </iframe>
</div>

<div>
    <h3>iPhone 8 landscape</h3>
    <iframe ID="iframe_iphone8_landscape" name="iframe_iphone8_landscape" onload="loadiframe(2)"
        src="http://localhost:3000/mainmenu.HTML" height="375" wIDth="667" class="iframe">
    </iframe>
</div>

<div>
    <h3>iPhone 8 porTrait</h3>
    <iframe ID="iframe_iphone8_porTrait" name="iframe_iphone8_porTrait" onload="loadiframe(3)"
        src="http://localhost:3000/mainmenu.HTML" height="667" wIDth="375" class="iframe">
    </iframe>
</div>

Js代码

<script>
const iframes = ['iframe_unit_landscape','iframe_unit_porTrait','iframe_iphone8_landscape','iframe_iphone8_porTrait'];

function loadiframe(ID) {
    let iframe = document.getElementByID(iframes[ID]); 

    for (var i = 0; i < iframes.length; i++) {
        if (i !== iframE) {
           var dest = document.getElementByID(iframes[i]).src = src;
        }
    }
};
</script>

解决方法

所以我在所有 event listener 中添加了一个 iframes,在加载时应该过滤 iframes 数组以删除没有相src 的帧,并设置 { {1}}。

因此,每当过滤后的 src 加载并运行该函数时,不会有其他 iframe 没有相同的 iframes 防止无限循环

src

const iframes = ['iframe_unit_landscape','iframe_unit_porTrait','iframe_iphone8_landscape','iframe_iphone8_porTrait'].map(id => document.getElementById(id));

iframes.forEach(iframe => iframe.addEventListener('load',_ => iframes.filter(frame => frame.src != iframe.srC).forEach(frame => frame.src = iframe.srC)));
@H_696_53@

大佬总结

以上是大佬教程为你收集整理的同步 iframe全部内容,希望文章能够帮你解决同步 iframe所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:同步