html實現iframe內嵌頁面跳轉的demo


記錄一下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很簡單,但還是感謝宿舍三哥對本萌新的點撥幫助


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM