更新时间:2022-10-21 22:34:25
忽略建议属性名是问题。 jQuery API明确声明可以接受任何一种符号: http://api.jquery.com/css/
实际的问题是你在这行上缺少一个大括号:
$(#myParagraph)css({backgroundColor:black,color:white);
更改为:
$(#myParagraph)。css({backgroundColor:black,color:white});
这是一个工作演示: http://jsfiddle.net/YPYz8/
I am trying to change the CSS using jQuery:
$(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>
What am I missing here?
Ignore the people that are suggesting that the property name is the issue. The jQuery API explicitly states that either notation is acceptable: http://api.jquery.com/css/
The actual problem is that you are missing a close curly brace on this line:
$("#myParagraph").css({"backgroundColor":"black","color":"white");
Change it to this:
$("#myParagraph").css({"backgroundColor": "black", "color": "white"});
Here's a working demo: http://jsfiddle.net/YPYz8/