美文网首页
JavaScript-DOM-getAttribute&

JavaScript-DOM-getAttribute&

作者: 报告老师 | 来源:发表于2017-09-28 09:08 被阅读9次

1.getAttribute方法可以获取Dom对象的属性值(主要用于获取自定义属性的值)

参数:属性名

返回值:对应属性值

<p id = "p1" class = "pc" data-type = "string"></p>

var pcls = document.getElementById(p1).getAttribute("class");

console.log(pcls);

输出结果:pc

2.setAttribute方法,用于设定Dom对象的属性

参数:属性名

<p id ="p2"></p>

var scls = document.getElementById("p2").setAttribute("class","pc");

Dom元素变成:

<p id =""p2 class = "pc"></p>

相关文章

  • JavaScript-DOM-getAttribute&

    1.getAttribute方法可以获取Dom对象的属性值(主要用于获取自定义属性的值) 参数:属性名 返回值:对...

网友评论

      本文标题:JavaScript-DOM-getAttribute&

      本文链接:https://www.haomeiwen.com/subject/wlhmextx.html