---
title: "getAttribute取得元素属性值"
author: "小丑鱼0703"
date: "2015-03-04T14:48:09Z"
tags: []
categories: ["Web前端"]
slug: "getattribute"
draft: false
lastmod: "2015-03-04T14:48:09Z"
id: 2188745
url: "https://wangjingyi.iteye.com/blog/2188745"
---

getAttribute(strAttributeName, lFlags）
取得属性值。


```html
<div id="div1" class="divname" title="div"></div> 
```


```js
var el = document.getElementById("div1");  
alert(el.getAttribute("id"));//div1
```

<https://msdn.microsoft.com/zh-cn/subscriptions/ms536429.aspx>中介绍了lFlags参数的意义：
0
Default. Performs a property search that is not case-sensitive, and returns an interpolated value if the property is found.

1 strAttributeName大小写完全匹配
Performs a case-sensitive property search. To find a match, the uppercase and lowercase letters in strAttributeName must exactly match those in the attribute name.

2
Returns attribute value as a String. This flag does not work for event properties.

4
Returns attribute value as a fully expanded URL. Only works for URL attributes.