前端外包优质服务商云客秀

我们凭借着对品牌的深刻理念,互联网营销趋势的敏锐洞察,帮助企业通过互联网建立优势。

当前位置:
首页>
荆州网站建设

JS动态变更样式

  • 2020-08-31

1、通过className属性更改class名

var child0 = document.createElement("div");

child0.innerHTML = "child0";

child0.className = "newDiv";

parent.appendChild(child0);

2、 通过classList属性更改class名

在html中,一个tag可以应用了多个类,如:

<div class="bg tex">……</div>

通过className属性去更改其中的一个类比较麻烦。

而使用classList属性则会很方便,如:

obj.classList.remove('bg');

3、更改style属性

4、直接更改样式表

菜单