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


HTML DOM hasAttributes()用法及代碼示例


DOM hasAttribute()方法是一個布爾函數,它返回true或false,但不返回兩者。如果元素包含屬性,則此方法返回true值,否則返回false。知道文檔中的元素是否具有屬性非常有用。

用法:

node.hasAttributes()

參數:它不包含任何參數。


返回值:如果元素包含屬性,則返回true,否則返回false。

範例1:

<!DOCTYPE html>  
<html>  
    <head>  
        <title> 
            DOM hasAttributes() Method 
        </title>  
    </head>  
      
    <body>  
        <center>  
            <h1 style = "color:green;width:50%;" id = "sudo">  
                GeeksForGeeks  
            </h1>  
              
            <h2>DOM hasAttributes() Method </h2>      
              
            <p>Click on the button to check if that body  
            element has any attributes</p> 
              
            <button type = "button" onclick = "geeks()">  
                Submit  
            </button>  
              
            <script>  
                function geeks() {  
                    var s = document.body.hasAttributes(); 
                    document.getElementById('gfg').innerHTML = s; 
                }  
            </script>  
              
            <p id = "gfg"></p> 
        </center>  
    </body>  
</html>                                

輸出:
在單擊按鈕之前:

單擊按鈕後:

範例2:

<!DOCTYPE html>  
<html>  
    <head id = "rk">  
        <title> 
            DOM hasAttributes() Method 
        </title>  
    </head>  
      
    <body>  
        <center>  
            <h1 style = "color:green;width:50%;" id = "sudo">  
                GeeksForGeeks  
            </h1>  
              
            <h2>DOM hasAttributes() Method </h2> 
              
            <p>Click on the button to check if that 
            head element has any attributes</p>      
  
            <button type = "button" onclick = "geeks()">  
                Submit  
            </button>  
              
            <script>  
                function geeks() {  
                    var s = document.head.hasAttributes(); 
                    document.getElementById('gfg').innerHTML = s; 
                }  
            </script>  
                <p id = "gfg"></p> 
        </center>  
    </body>  
</html>                    

輸出:
在單擊按鈕之前:

單擊按鈕後:

支持的瀏覽器:下麵列出了DOM hasAttributes()方法支持的瀏覽器:

  • 穀歌瀏覽器
  • Internet Explorer 9.0
  • 火狐瀏覽器
  • Opera
  • 蘋果瀏覽器


相關用法


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