JS:用js读取、设置元素属性

setAttribute()方法

添加指定的属性,并为其赋指定的值

如果这个指定的属性已经存在,则仅更改值;

elementObject.setAttribute(attributename, attributevalue);

参数:

attributename:string.必需。属性名称

attributevalue:string.必需。属性值

返回值无返回值

例子:

document.getElementsByTagName("input")[0].setAttribute('type', 'button');

详情

getAttribute()

原型:

element.getAttribute(attributenamestring)

参数:

attributenamestring:字符串。必需。需要获得其属性的属性名称

返回值

若属性存在,string,指定属性的值

若属性不存在,返回null

 

posted @ 2016-06-28 13:41  Carolina  阅读(419)  评论(0编辑  收藏  举报