原生js操作css:obj.style.cssText

js 修改 css 最简单的方法,就是直接修改 cssText

d.style.cssText='css属性1:css值1; css属性2:css值2;'

一次可以写多个值,每个值用 , 分割,就像在标签的 style 属性,无任何语法不同。

它也直接返回多个css,仍然是以 ; 分割:

<h2 id="hello" style="position:fixed;color:red;"></h2>

<script>

    let a = document.getElementById('hello');
    console.log(a.style.cssText);
    
</script>

打印结果:

position: fixed; color: red;

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注