当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML DOM close()用法及代码示例


DOM close()方法用于关闭输出流。它用于指示在窗口上完成写入。 close()方法不需要任何参数。

用法:

document.close()

范例1:


<!DOCTYPE html> 
<html> 
    <head> 
        <title>DOM close Method</title> 
        <style> 
            h1, h2{ 
            color:green; 
        } 
        body { 
            text-align:center; 
        } 
        </style> 
    </head> 
    <body> 
        <h1>GeeksForGeeks</h1> 
        <h2>DOM close() Method</h2> 
        <button onclick="geeks()">Submit</button> 
        <script> 
  
            // This function is called on submit, it opens 
            // a stream, write "PRACTICE" and then closes 
            // the stream. 
            function geeks() { 
                document.open(); 
                document.write("<h1>PRACTICE</h1>"); 
                document.close(); 
            } 
        </script> 
    </body> 
</html>                    

输出:

范例2:本示例使用window.close方法在新窗口中显示输出。

<!DOCTYPE html> 
<html> 
    <head> 
        <style> 
            h1, h2 { 
                color:green; 
            } 
            body { 
                text-align:center; 
            } 
        </style> 
    </head> 
    <body> 
        <h1>GeeksForGeeks</h1> 
        <h2>DOM close() Method</h2> 
        <button onclick="geeks()">Submit</button> 
        <script> 
  
            // This function is called on submit, it opens 
            // a new window and a stream, write "PRACTICE" and  
            // then closes the stream. 
            function geeks() { 
                var gfg = window.open(); 
                gfg.document.open(); 
                gfg.document.write("<h1>PRACTICE</h1>"); 
                gfg.document.close(); 
            } 
        </script> 
    </body> 
</html>                    

输出:

支持的浏览器:下面列出了DOM close()方法支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • Opera
  • 苹果浏览器


相关用法


注:本文由纯净天空筛选整理自ManasChhabra2大神的英文原创作品 HTML | DOM close() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。