从同一页面中嵌入的另一个iframe更改iframe源的问题

时间:2013-01-29 09:24:44

标签: javascript html iframe src

我将两个iFrame嵌入到单个父页面中。一个是导航栏,另一个是内容框架。

我需要使用导航框内的按钮更改内容框架的来源,但是我发现此刻很难实现,我不知道为什么。

以下是父页面的正文来源:

<body>

<script type="text/javascript">

var links = [
'secondary.html',
'default.html'
];
var one, two;
window.onload=function() {
  one = document.getElementById('content');
  two = document.getElementById('content');
} 

function onenav(idx) {
    one.src=links[idx];
}

function twonav(idx) {
two.src=links[idx];
}

</script>
<iframe src='sidebar.html' name='sidebar' scrolling="yes" height="600" width="250"     seamless="seamless" frameborder="1" align="left">
</iframe>

<iframe src='default.html' name='content' scrolling="yes" height="600" width="800" seamless="seamless" frameborder="1" align="left"></iframe>

</body>

这是侧边栏页面的来源:

<button onclick="parent.onenav(0)">Secondary</button>
<button onclick="parent.twonav(1)">Default</button>

内容框架没有相关的html代码。

1 个答案:

答案 0 :(得分:2)

您错过了iframe的id属性。使用

<iframe src='default.html' id="content" name='content' scrolling="yes" height="600" width="800" seamless="seamless" frameborder="1" align="left"></iframe>

它有效。