如果要设置元素中除style之外的其他属性(如:align、title等),应使用Element对象专用于DOM的方法。
可以用来获取和修改HTML元素属性的内容的三个方法是getAttribute()、setAttribute()和removeAttribute()。
Element对象的方法 | 说明 |
---|---|
getAttribute(attributeName) | 返回所提供的属性的值;如果该属性不存在,就返回null或空字符串 |
setAttribute(attributeName,value) | 传递两个参数(属性名,属性值),设置属性的值 |
removeAttribute(attributeName) | 删除属性的值,代之以默认值 |