當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。