前一个cg.setAttribute("align", "center");可以达到居中的效果;
但是后面的cg.setAttribute("color", "navy");无法达到更改颜色的目的。
而最后浏览器也alert了color值是navy...
源代码:
what the fuck?
what the fuck?
what the fuck?
what the fuck?
what the fuck?
var cg=document.getElementById("box");
alert(cg.getAttribute("align"));
cg.setAttribute("align", "center");
alert(cg.getAttribute("align"));
cg.setAttribute("color", "navy");
alert(cg.getAttribute("color"));
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
setAttribute操作的是HTML标签的属性。。color是css属性,是在style属性里的。。你这样直接.setAttribute('color',xxx)出来就变成:
应该是:
或者:
或者:
或者:
首先,setAttribute这个方法是设置标签的属性的。而p只有一个属性:align,所以你的cg.setAttribute("align", "center")才会生效。p介绍
然后,你要设置的color是标签的样式,只能通过设置css来实现效果,当然你直接设置一个color属性,标签是接收到了,但是不会去渲染,因为p没有color这个属性。
最后,要修改样式,请统一使用css样式来设置。比如: