How to clear the content of an IFRAME?

2019-02-02 22:19发布

How do I clear the content of my IFRAME element, using javascript, without loading a blank page into it?

I can figure out to do this: iframe_element.src = "blank.html", but there must be a better, instant, method.

10条回答
啃猪蹄的小仙女
2楼-- · 2019-02-02 22:57

You could also do this:

<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>
查看更多
爷、活的狠高调
3楼-- · 2019-02-02 22:57

$("#iframe").contents().find("body").html('');

查看更多
迷人小祖宗
4楼-- · 2019-02-02 22:59
var iframe = document.getElementById("myiframe");
var html = "";

iframe.contentWindow.document.open();
iframe.contentWindow.document.write(html);
iframe.contentWindow.document.close();

tested in IE, Firefox and Chrome ... it did it :)

查看更多
别忘想泡老子
5楼-- · 2019-02-02 23:00
about:blank

is a "URL" that is blank. It's always clear

You can set the page's source to that, and it will clear.

查看更多
登录 后发表回答