定义和用法
setAttribute() 方法创建或改变某个新属性。
如果指定属性已经存在,则只设置该值。
浏览器支持:
所有主要浏览器都支持 setAttribute() 方法
Internet Explorer 8 及更早 IE 版本不支持该方法。
语法
element.setAttribute(attributename,attributevalue)参数
| 参数 | 类型 | 描述 |
|---|---|---|
| attributename | String | 必须。你要添加的属性名称。 |
| attributevalue | String | 必须。你要添加的属性值。 |
返回值
没有返回值。
技术细节
| DOM 版本 | Core Level 1 Element Object |
|---|
实例
设置 input元素的的属性类型:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>
<input value="OK">
<p id="demo">点击下面的按钮来设置按钮的类型属性。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
document.getElementsByTagName("INPUT")[0].setAttribute("type","button");
};
</script>
<p>Internet Explorer 8 及更早的版本不支持 setAttribute 方法。</p>
</body>
</html>作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/4401.html
支付宝
微信