Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to change the CSS using jQuery:(我正在尝试使用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> 

What am I missing here?(我在这里想念什么?)

  ask by user449914 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
190 views
Welcome To Ask or Share your Answers For Others

1 Answer

Ignore the people that are suggesting that the property name is the issue.(忽略提示属性名称是问题的人员。)

The jQuery API documentation explicitly states that either notation is acceptable: http://api.jquery.com/css/(jQuery API文档明确声明了两种表示形式均可接受: http : //api.jquery.com/css/)

The actual problem is that you are missing a closing 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/(这是一个工作示例: 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> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...