您可以使用jQuerycss()方法将新CSS属性添加到元素,或使用jQuery动态修改现有属性值。
让我们尝试以下示例,以了解此方法的基本原理:
例试试这个代码»
<!DOCTYPE html>
<html lang="en">
<head>
<title>jQuery Add CSS Property Dynamically</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
$("h1").css("color", "red");
$("p").css({
"background-color": "yellowgreen",
"font-weight": "bold"
});
});
</script>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
如果您觉得本文的内容对您的学习有所帮助:
关键字:
jQuery