記錄一下iframe內嵌頁面的跳轉方式
代碼:
index.html:
<html> <head> <title>根目錄</title> <script> function changeSrc1() { document.getElementById("iframe1").setAttribute("src","1.html"); } function changeSrc2() { document.getElementById("iframe1").setAttribute("src","2.html"); } </script> </head> <body> <div> <button id="btn-sub1" href="1.html" target="iframe1" onclick="changeSrc1()">切換到頁面一</button> <button id="btn-sub2" href="2.html" target="iframe1" onclick="changeSrc2()">切換到頁面二</button> <a href="1.html" target="iframe1">切換到頁面一</a> <a href="2.html" target="iframe1">切換到頁面二</a> </div> <div> <iframe name="iframe1" id="iframe1" src="1.html"> </iframe> </div> </body> </html>
1.html:
<html> <head> <title>根目錄</title> </head> <body> <div> <h1>頁面一</h1> </div> </body> </html>
2.html:
<html> <head> <title>根目錄</title> </head> <body> <div> <h1>頁面二</h1> </div> </body> </html>
三個頁面在同一文件夾內
注:target指向的是iframe的name,不是id
demo:https://files.cnblogs.com/files/DoubleBarbecue/iframe.rar
另:雖然這個demo很簡單,但還是感謝宿舍三哥對本萌新的點撥幫助
