0

iframe.main と iframe.secondary の 2 つの iframe を含む Web ページがあります。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ボタンのクリック時に 2 つの 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 に答える