从另一个iframe更改iframe源代码 [英] Change iframe source from another iframe
问题描述
好的,我在父页面内有2个iframe(无论出于何种原因)。
Ok, I have 2 iframes inside a parent page (for whatever reason).
我在父页面上有一个导航菜单,它改变了iframe的来源# 1 ...
I have a navigation menu on parent page, which changes the source of iframe #1...
iFrame#1的工作,是显示另一个导航菜单...就像一个子导航菜单......
iFrame #1's job, is to display ANOTHER navigation menu... Like a subnavigation menu...
现在,如何点击iFrame#1中的li,更改iframe#2的来源?它们都在同一个父页面上...
Now, how can I upon clicking an li inside iFrame #1, change the source of iframe #2? They're both on the same parent page...
除了惨遭失败之外,我还收到Chrome开发工具的警告 -
Aside from failing miserably, I also get a warning from Chrome's Dev tools -
不安全的JavaScript尝试使用URL文件访问框架:/// C:/website/index.html来自带有URL文件的框架:/// C:/website/news/navigator.html。域,协议和端口必须匹配。
Unsafe JavaScript attempt to access frame with URL file:///C:/website/index.html from frame with URL file:///C:/website/news/navigator.html. Domains, protocols and ports must match.
这里有一些代码可以使事情变得更加清晰:
Here's some code to make things slightly clearer:
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;
}
对我而言,这是有道理的,因为这一切都在父母身上执行page ...在加载时,我查询开发工具,我可以看到,'one'和'two'都有框架元素......第一个按钮工作,第二个按钮不工作......
To me, this makes sense, since this is all being executed on the parent page... On loading, I query the dev tools and I can see that both, 'one' and 'two' have frame elements... The first button works, the second one, doesn't...
推荐答案
使用parent.twonav时适用于我
Works for me when using 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];
}
这篇关于从另一个iframe更改iframe源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!