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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。