jq如何设置css

文章2022-05-1462 人已阅来源:网络

jq设置css的方法:1、设置单个样式属性,代码为【$('#test').css('background-color','red')】;2、设置多个属性你,可以传入一个对象,代码为【$('#test').css({'background】。

jq如何设置css

本教程操作环境:windows7系统、css3版,DELL G3电脑。

jq设置css的方法:

如果要设置单个样式属性可以这样

$('#test').css('background-color','red');

如果你要同时设置多个属性你,可以传入一个对象

$('#test').css({'background-color':'red','font-size':'20px'});

即属性值例如background-color,你也可以用驼峰形式backgroundColor,这时你可以不用单引号

你可以亲自试一下

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/<a href="http://www.jquerycn.cn/
" target="_blank" class="inner-link">jquery</a>/1.7.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $('#test').css('background-color','red');
  });
});
</script>
</head>
<body>
<h1 id="test">jquery 设置css</h1>
<button type="button">设置h1的css</button>
</body>
</html>

相关教程推荐:CSS视频教程

以上就是jq如何设置css的详细内容!