iframe

iframe 用于在網(wǎng)頁內(nèi)顯示網(wǎng)頁。

添加iframe語法

<iframe src="URL"></iframe>
URL 指向隔離頁面的位置。

Iframe - 設置高度和寬度

height 和 width 屬性用于規(guī)定 iframe 的高度和寬度。
屬性值的默認單位是像素,但也可以用百分比來設定(比如 "80%")。

<iframe src="demo_iframe.htm" width="200" height="200"></iframe>

Iframe - 刪除邊框

frameborder 屬性規(guī)定是否顯示 iframe 周圍的邊框。
設置屬性值為 "0" 就可以移除邊框:

<iframe src="demo_iframe.htm" frameborder="0"></iframe>

使用 iframe 作為鏈接的目標

iframe 可用作鏈接的目標(target)。
鏈接的 target 屬性必須引用 iframe 的 name 屬性:

<iframe src="demo_iframe.htm" name="iframe_a"></iframe>
<p><a target="iframe_a">W3School.com.cn</a></p>

可選屬性

image

iframe透明

在ie6/7/8下引入iframe的時候,它的背景默認是白色,即使設置了style=”background-color:transparent;”也無效,
但是其他瀏覽器(firefox,chrome,opera,ie9)都正常顯示,要解決這個兼容性問題,必須用到一個屬性。

給iframe設置屬性:allowTransparency=”true” //設置為true允許透明,就可以解決

<body style="background-color:#00f;">
<iframe allowTransparency="true" frameborder="0" height="200" width="200" src="son.html"
scrolling="yes" id="myiframe"></iframe>
</body>

高度自適應和父子之間通信,兄弟之間通信。

iframe.html
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <input type="button" value="調(diào)用子級方法" id="btn">
    <div class="main">
        <iframe src="index.html" frameborder="0" scrolling="no" name="win" id="win"></iframe>
    </div>
    
</body>
<script>
    //解決高度自適應
    function setIframeHeight(iframe) {
    if (iframe) {
        //獲取子級window
        var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
            if (iframeWin.document.body) {
                //獲取子級的高度賦值給iframe
                iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
            }
        }
    };

    window.onload = function () {
        setIframeHeight(document.getElementById('win'));
    };
    var btn = document.getElementById('btn');
    var win = document.getElementById('win');

    btn.onclick = function(){
        win.contentWindow.hello();
    }

    function say(){
        alert('ok')
    }

    function can(){
        win.contentWindow.canS = 'hello'
    }
    can();
</script>
</html>
index.html
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>

    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>

    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>

    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>

    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>

    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>

    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>

    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>

    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>

    <p>主要</p>
    <p>主要</p>
    <p>主要</p>
    <p>主要</p>

    <p>主要1111111111111111111111111111</p>
    <button id="btn">調(diào)用父級方法</button>
    <button id="btn1">獲取父級參數(shù)</button>
</body>
<script>
//console.log(document.domain)
//document.domain = 'demo.com';
    var btn = document.getElementById('btn');
    var btn1 = document.getElementById('btn1');
    btn.onclick = function(){
        parent.say();
    }
    function hello(){
        alert('yes')
    }

    function getcan(){
        alert(window.canS)
    }
    btn1.addEventListener('click',getcan,false);
</script>
</html>
tab.html
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <p>我的</p>
    <button id="btn2">兄弟之間的調(diào)用</button>
</body>
<script>
    //ar win = document.getElementById('win');
    var btn2 = document.getElementById('btn2');
    function getcan1(){
        //先獲取父級,然后接著找到對應的iframe
        console.log(parent.win.contentWindow.hello())
    }
    btn2.addEventListener('click',getcan1,true);
</script>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • HTML標簽解釋大全 一、HTML標記 標簽:!DOCTYPE 說明:指定了 HTML 文檔遵循的文檔類型定義(D...
    米塔塔閱讀 3,533評論 1 41
  • 2017-09-07摘抄自W3school-HTML Iframe希望幫助自己系統(tǒng)地打好基礎,也能在做筆記的同時添...
    moralok閱讀 553評論 0 0
  • iframe 用于在網(wǎng)頁內(nèi)顯示網(wǎng)頁。 添加iframe的語法 URL 指向隔離頁面的位置 Iframe - 設置高...
    Leathy閱讀 413評論 0 0
  • 本文我們用 Python 來實現(xiàn)一個client-Server的簡單的程序,在開始寫具體的程序之前,我們看一下網(wǎng)絡...
    風影無忌閱讀 1,268評論 0 5
  • 第一次接觸到交替性暴食厭食癥,是在饒雪漫的小說中。莫醒醒,暴食癥發(fā)作時,可以吞下數(shù)量驚人的食物,不管生熟,不論品種...
    玫子卿茶閱讀 876評論 2 2

友情鏈接更多精彩內(nèi)容