/ / 2つの別々のiframeに2つの別々のページを読み込む方法 - iframe

2つの別々のページを2つの別々のiframeに読み込む方法 - iframe

私は2つのiframeを持つWebページを持っています:iframe。メインと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イベントでそれを行うことはできますか?

回答:

回答№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>