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


HTML DOM defaultView用法及代碼示例


HTML中的DOM defaultView屬性用於返回文檔Window Object。窗口對象是瀏覽器中打開的窗口。

用法:

document.defaultView 

返回值:它用於返回當前的Window對象。


範例1:

<!DOCTYPE html> 
<html> 
    <head> 
        <title> 
            DOM defaultView Property 
        </title> 
    </head> 
      
    <body style = "text-align:center"> 
          
        <h1 style = "color:green">GeeksForGeeks</h1> 
        <h2>DOM defaultView Property </h2> 
          
        <button onclick = "geeks()">Submit</button> 
          
        <p id="sudo"></p> 
          
        <script> 
            function geeks() { 
                var doc = document.defaultView; 
                document.getElementById("sudo").innerHTML = doc; 
            } 
        </script> 
      
    </body> 
</html>                    

輸出:
之前單擊按鈕:

單擊按鈕後:

範例2:本示例用於返回Windows的寬度和高度。

<!DOCTYPE html> 
<html> 
    <head> 
        <title> 
            DOM defaultView Property 
        </title>  
    </head> 
      
    <body> 
        <center> 
            <h1 style = "color:green;">GeeksForGeeks</h1> 
            <h2>DOM defaultView Property </h2> 
              
            <button onclick="Geeks()">Submit</button> 
              
            <p id="sudo"></p> 
              
            <!-- script to find window size -->
            <script> 
                function Geeks() { 
                    var def_view = document.defaultView; 
                    var width = def_view.innerWidth; 
                    var height = def_view.innerHeight; 
                    document.getElementById("sudo").innerHTML  
                    = "Width:" + width +  
                    "<br>Height:" + height; 
                } 
            </script> 
        </center> 
    </body> 
</html>                    

輸出:
之前單擊按鈕:

單擊按鈕後:

支持的瀏覽器:下麵列出了DOM defaultView屬性支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • 火狐瀏覽器
  • Opera
  • 蘋果瀏覽器


相關用法


注:本文由純淨天空篩選整理自ManasChhabra2大神的英文原創作品 HTML | DOM defaultView Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。