css()方法用于读取或设置元素样式:里面可以是对象。
代码示例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery的css方法</title>
<script type="text/javascript" src="../js/jquery-3.1.1/jquery-3.1.1.min.js" ></script>
<script type="text/javascript">
$(function(){
//得到第一个p标签的颜色
console.log($("p:first").css("color"));
//设置所有p标签的文本颜色为红色
// $("p").css("color","red");
//设置第二个p标签的字体颜色(#ff0011),背景(blue),宽(300px),高(30px)
$("p:last").css({
color:'#ff0011',
backgroundColor:'blue',//非数值的值要加引号
width:300,//不能加px,否则会报错
height:30
});
// $("p:eq(1)").css({
// color:'#ff0011',
// backgroundColor:'blue',
// width:300,
// height:30
// });
});
</script>
</head>
<body>
<p style="color: darkblue;">热爱可抵岁月漫长</p>
<p style="color: greenyellow;">温柔可挡艰难时光</p>
</body>
</html>