我正在设置一个 iframe 来嵌入来自服务器的流视频,它运行得很好。 当用户单击标签来更新 iframe 并更改其来源时,我需要它。 它在我的桌面上的不同浏览器上也运行良好,但在移动设备上不起作用。 用户单击标签后,iframe 变为空并仅在移动设备浏览器上显示(未找到)此情况。 有什么解决办法吗?
我搜索了很多答案,所有这些都为移动浏览器提供了相同的结果。 我还尝试使用javascript函数来更新iframe的源。
iframe:
<iframe name="iframe1" id="frameid" style="width:600px; height:480px;" allowfullscreen src="http://serverip:8080/cc/embed.html"></iframe>
将更新 iframe 源的链接:
<a href="#" onclick="update('http://serverip:8080/test.html')">
JavaScript 函数:
<script>
function update(loc) {
document.getElementById('frameid').src = loc;
}
</script>
我也尝试重新加载 iframe,但它也不起作用:
function update2(loc) {
document.getElementById('frameid').src = loc;
document.getElementById('frameid').contentWindow.location.reload(true);
}
我还尝试在单击链接时创建一个新的 iframe,它创建了一个新的 iframe,但也是空的,也显示(未找到)。
最佳答案
你可以这样做:
<a href="http://www.google.com/" onclick="return loadIframe(this.href);">Page 1</a>
<a href="http://www.apple.com/" onclick="return loadIframe(this.href);">Page 2</a>
<iframe name="frameid" id="frameid" src="http://www.microsoft.com/" frameborder="0">
Your browser doesn't support iframes.
</iframe>
function loadIframe(url) {
$("#frameid").attr('src',url);
return false;
}
关于javascript - 如何使用 javascript 更改 iframe 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57640929/