该css()方法可以将属性名称和值作为单独的参数来为元素设置单个CSS属性。基本语法可以通过以下方式给出:
$(selector).css(“ propertyName”,“ value”);
以下示例将在单击元素background-color时将<div>元素的CSS属性设置为color值 blue。
例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery css() Demo</title>
<style>
.box{
width: 100px;
height: 100px;
display: inline-block;
border: 1px solid #cdcdcd;
margin: 10px;
}
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$(".box").click(function(){
$(this).css("background-color", "blue");
});
});
</script>
</head>
<body>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</body>
</html>
如果您觉得本文的内容对您的学习有所帮助:
关键字:
jQuery