---
title: "js-dom检查元素属性 hasAttribute()"
author: "小丑鱼0703"
date: "2015-03-04T13:56:57Z"
tags: []
categories: ["Web前端"]
slug: "js-dom-hasattribute"
draft: false
lastmod: "2015-03-04T13:57:13Z"
id: 2188730
url: "https://wangjingyi.iteye.com/blog/2188730"
---

**hasAttribute() **
  如果存在指定属性，则 hasAttribute() 方法返回 true，否则返回 false。

<http://www.w3school.com.cn/jsref/met_element_hasattribute.asp>
浏览器兼容性：
  1.所有主流浏览器均支持 hasAttribute() 方法。
  2.Internet Explorer 8 以及更早的版本不支持该方法。

判断按钮是否有点击事件：

```javascript

document.getElementsByTagName("button")[0].hasAttribute("onclick");//结果：true
```