代码之家  ›  专栏  ›  技术社区  ›  sikender

如何清除IFRAME的内容?

  •  59
  • sikender  · 技术社区  · 16 年前

    我可以想办法做到这一点: iframe_element.src = "blank.html"

    10 回复  |  直到 10 年前
        1
  •  109
  •   McKay    16 年前
    about:blank
    

    是一个空白的“URL”。总是很清楚的

    您可以将页面的源设置为该值,它将被清除。

        2
  •  16
  •   robsch YaakovHatam    10 年前
    var iframe = document.getElementById("myiframe");
    var html = "";
    
    iframe.contentWindow.document.open();
    iframe.contentWindow.document.write(html);
    iframe.contentWindow.document.close();
    

        3
  •  8
  •   AnthonyWJones    16 年前

        4
  •  3
  •   Khalid    15 年前

    或者您可以这样做:

    var iframe_element = window.frames['iframe_name'];
    iframe_element.document.open();
    iframe_element.document.close();
    
        5
  •  2
  •   Pekka    16 年前

    在有很多iFrame的页面上,我很难使用“about:blank”。它似乎不是每个浏览器中的一个有效位置(尽管我从未确定)。不管怎样,我对你很满意 javascript:void(0);

        6
  •  2
  •   Jared xxtesaxx    16 年前

    您也可以这样做:

    <html>
    <head>
    <script>
        var doc = null;
        window.onload = function() {
            alert("Filling IFrame");
            doc = document.getElementById("test");
    
            if( doc.document ) {
                document.test.document.body.innerHTML = "<h1>test</h1>"; //Chrome, IE
            }else {
                doc.contentDocument.body.innerHTML = "<h1>test</h1>"; //FireFox
            }
    
                setTimeout(function() { 
                    alert("Clearing IFrame");
    
                    if( doc.document ) {
                        document.test.document.body.innerHTML = ""; //Chrome, IE
                    }else {
                        doc.contentDocument.body.innerHTML = ""; //FireFox
                    }
    
                }, 1000);
            };
        </script>
    </head>
    
    <body>
        <iframe id="test" name="test">
    
        </iframe>
    </body>
    </html>
    
        7
  •  2
  •   Keith    9 年前

    //首先,我从它的Id获取窗口。

    var my_content = document.getElementById('my_iframe').contentWindow.document;
    

    my_content.body.innerHTML="";
    

    //现在,当我编写新内容时,它不会附加到正文的末尾,iframe正文将只包含新内容。

    my_content.write(new_content);
    
        8
  •  0
  •   Mario Petrovic Emmanuel Onah    5 年前

    想做就做:

    var iframe = document.getElementById("iframe");
    iframe.removeAttribute('srcdoc');
    

        9
  •  -1
  •   Manas    10 年前

    只需获取Iframe并从中删除documentElement。Iframe将为空

     var frame = document.getElementById("YourFrameId"),
     frameDoc = frame.contentDocument || frame.contentWindow.document;
     frameDoc.removeChild(frameDoc.documentElement);
    
        10
  •  -2
  •   TheLethalCoder    9 年前
    function getContentFromIframe(iFrameName)
    {
        var myIFrame = document.getElementById(iFrameName);
        var content = myIFrame.contentWindow.document.body.innerHTML;
    
        //Do whatever you need with the content    
    }
    
        11
  •  -4
  •   techknowfreak    12 年前

    $(“#iframe”).contents().find(“body”).html(“”);