jQuery的css函数设置

$(selector).css(name,function(index,value))

name:必需。规定 CSS 属性的名称

function(index,value):

规定返回CSS属性新值的函数。

  • index - 可选。接受选择器的index位置
  • value - 可选。接受CSS属性的当前值
<body>
    <div class="a2">
        <p>阿道夫 </p>
    </div>
    <script>
        $(function(){
            $(".a2").css("background","red");

        })
    </script>
</body>

这样能设置成功,但是我们加上 !important

$(".a2").css("background","red");
$(".a2").css("background","red !important");

jQuery就会设置失效

但是换种写法

$(".a2").css("cssText","background:red !important");

就能成功

cssText的用法以及特点

  cssText 的本质就是设置 HTML 元素的 style 属性值。

  obj.style.cssText=”样式”;

document.getElementById("a2").style.cssText = "color:red; font-size:13px;";

在jQuery里就是

$("选择器").css("cssText", "样式")

猜你喜欢

转载自www.cnblogs.com/sad-dog/p/12672600.html