0

我有一个包含两个 iframe 的网页:iframe.main 和 iframe.secondary。我想知道是否有办法在 iframe.main 中的页面加载时将特定页面加载到 iframe.secondary ?我将尝试说明我想要实现的目标:

<body>

 <iframe id="main" src="">

 </iframe>

 <iframe id="secondary" src="">

 </iframe>

 <button onClick="main.location.href='mainpage.html'">
  Load mainpage.html to iframe.main and secondary.html to iframe.secondary
 </button>

</body>

那么,当 mainpage.html 加载到 iframe.main 时,如何将 secondary.html 加载到 iframe.secondary?我可以使用按钮的 onClick 事件或 mainpage.html 的 onLoad 事件吗?

4

1 回答 1

0

在按钮单击时更改/设置src两个 iframe 的属性。下面是一个示例,它也使您的 HTML 更精简:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Two iframes</title>
<script type='text/javascript'>
window.onload = function(){
  // Get the button that will trigger the action
  var b = document.getElementById('trigger');
  // and set the onclick handler here instead of in HTML
  b.onclick = doLoads;

  // The callback function for the onclick handler above
  function doLoads() {
      // Get the two iframes
      var m = document.getElementById('main');
      var s = document.getElementById('secondary');
      // and set the source URLs
      m.src = "mainpage.html";
      s.src = "secondary.html";
  }

  // You could also move doLoads() code into an anonymous function like this:
  //     b.onclick = function () { var m = ... etc. }
} 
</script>
</head>
<body>
<iframe id="main" src=""></iframe>
<iframe id="secondary" src=""></iframe>
<br>
<button id="trigger">Load both pages</button>
</body>
</html>
于 2013-06-03T12:18:25.550 回答