我正在尝试使用 jQuery 更改 CSS:
$(init);
function init() {
$("h1").css("backgroundColor", "yellow");
$("#myParagraph").css({"backgroundColor":"black","color":"white");
$(".bordered").css("border", "1px solid black");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="bordered">
<h1>Header</h1>
<p id="myParagraph">This is some paragraph text</p>
</div>
我在这里错过了什么?
最佳答案
忽略那些暗示属性名称是问题的人。 jQuery API 文档明确指出可以接受任何一种表示法:http://api.jquery.com/css/
实际问题是您在这一行中缺少右大括号:
$("#myParagraph").css({"backgroundColor":"black","color":"white");
改成这样:
$("#myParagraph").css({"backgroundColor": "black", "color": "white"});
这是一个工作演示:http://jsfiddle.net/YPYz8/
$(init);
function init() {
$("h1").css("backgroundColor", "yellow");
$("#myParagraph").css({ "backgroundColor": "black", "color": "white" });
$(".bordered").css("border", "1px solid black");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="bordered">
<h1>Header</h1>
<p id="myParagraph">This is some paragraph text</p>
</div>
关于javascript - 如何使用 jQuery 更改 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3730035/