好的,我在父页面中有 2 个 iframe(无论出于何种原因)。
我在父页面上有一个导航菜单,它更改了 iframe #1 的源...
iFrame #1 的工作是显示另一个导航菜单...就像子导航菜单...
现在,我如何在点击 iFrame #1 中的 li 后更改 iframe #2 的源?它们都在同一个父页面上...
除了惨败之外,我还收到了来自 Chrome 开发工具的警告 -
不安全的 JavaScript 尝试从具有 URL file:///C:/website/news/navigator.html 的框架访问具有 URL file:///C:/website/index.html 的框架。域、协议(protocol)和端口必须匹配。
这里有一些代码可以让事情变得更清晰:
HTML
<!-- HTML for the parent page itself -->
<iframe id="frameone" src=""></iframe>
<iframe id="frametwo" src=""></iframe>
<button onclick="onenav('test.html')">Change 1st frame</button>
<!-- The following is the HTML that is loaded inside "frameone" -->
<button onclick="twonav('test2.html')">Change 2nd frame</button>
// Javascript
var one = document.getElementById('frameone');
var two = document.getElementById('frametwo');
function onenav(x){
one.src = x;
}
function twonav(y){
two.src = y;
}
对我来说,这是有道理的,因为这一切都在父页面上执行...加载时,我查询开发工具,我可以看到“一”和“二”都有框架元素。 . 第一个按钮有效,第二个按钮无效...
最佳答案
在使用 parent.twonav 时对我有用
var links = [
'javascript:\'<button onclick="parent.twonav(1)">Change 2nd frame</button>\'',
'javascript:\'Hello\''
];
var one, two;
window.onload=function() {
one = document.getElementById('frameone');
two = document.getElementById('frametwo');
}
function onenav(idx) {
one.src=links[idx];
}
function twonav(idx) {
two.src=links[idx];
}
关于javascript - 从另一个 iframe 更改 iframe 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6166357/