关于创建属性节点的问题

来源:9-16 创建元素节点createElement

给大佬递茶

2018-11-15 22:26

a是一个标签,有三种创建属性的方式

//a.color="blue";可以

a.setAttribute("color","blue");//可以

//var attribute=document.createAttribute("color");//返回是一个属性节点

//attribute.value="blue";

//a.appendChild(attribute);//这种方式明显不行

//那么如何将attribute放到a标签中?????


写回答 关注

1回答

  • 慕侠2155777
    2018-12-21 16:44:55

    a.style.color='red';

    a.setAttribute('style','color:red');

    color不是标签属性,style是属性,class是属性  color:red 这是style的内容  而且appendChild是把元素放入另个元素中,不是把属性放入元素中

    var attribute=document.createAttribute("style");//注意这里!!!

    attribute.value="color:blue";//这才是style属性的完整值;

    a.appendChild(attribute);-------a.setAttributeNode(attribute);//这是给标签添加属性(class,style,name,value,type等)

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

467395 学习 · 21877 问题

查看课程

相似问题