js中的getAttribute()

来源:互联网 发布:二级c语言成绩查询时间 编辑:程序博客网 时间:2024/06/07 18:56
  • getAttribute()是HTML DOM的一个方法,用以获取HTML元素的属性(如id,name,type以及其他自定义属性)。同理,setAttribute()、removeAttribute()都是对HTML元素的属性进行操作。
  • style是HTML DOM的一个关于样式的对象,style对象的属性(如background,color,border等等)用以设置元素的样式
  • 举例说明:
  
<input type="text" id="btn" style="background-color: red"> 
  1.     获取type属性可以用: document.getElementById("btn").getAttribute("text"); //注意要获取的属性有双引号
  2.   获取元素样式可以用: document.getElementById("btn").style.backgroundColor;
  • 注: 如果属性是内联样式,也可直接用getAttribute()来获取其属性,但只能获取style,无法设置style里面具体的background-color里面具体的值。
                  利用.getAttribute("style")得到的结果是 background-color: red