admin管理员组文章数量:1289944
I have jQuery on my webpage.
I am changing background color of a div.
Its not working.
<script type="text/javascript">
$("#gridbox").css({'background-color':'black'});
</script>
I have jQuery on my webpage.
I am changing background color of a div.
Its not working.
<script type="text/javascript">
$("#gridbox").css({'background-color':'black'});
</script>
Share
Improve this question
edited Oct 3, 2012 at 8:13
Darren
70.8k24 gold badges140 silver badges145 bronze badges
asked Sep 13, 2012 at 9:56
Hardik FefarHardik Fefar
3214 silver badges20 bronze badges
5
- I think it's $("#gridbox").css({'background-color','black'}); - ma not colon – Carlo Moretti Commented Sep 13, 2012 at 9:57
- 1 @Onheiron: no, the colon is correct. Note its in curly brackets so its not ma separated parameters, its properties being set (JSON style). – Chris Commented Sep 13, 2012 at 10:00
-
@Onheiron You're getting confused with
$("#gridbox").css('background-color','black');
– Curtis Commented Sep 13, 2012 at 10:01 - <div id="gridbox"> <div class="friend cell"> <div class="inner"> <span class="name">Bob</span> <br /> (id: 57) <input type="hidden" class="friend_id" value="57 " /> </div> </div> </div> – Hardik Fefar Commented Sep 13, 2012 at 10:02
- Can you provide the html and the css you used with this jquery code please ? – Charles Jourdan Commented Sep 13, 2012 at 13:02
5 Answers
Reset to default 6Your code should work, try using document ready handler.
$(document).ready(function(){
$("#gridbox").css({'background-color':'black'});
})
http://jsfiddle/WcQse/
Its possible your script is running before the DOM has loaded. Try:
<script type="text/javascript">
$(document).ready(function(){
$("#gridbox").css({'background-color':'black'});
});
</script>
-- Demo --
Try this
If you are using ASP.NET
then your Client ID
might change depending on the use of Master Pages
in that case you can try something like this.
$('#<%= gridbox.ClientID %>' ).css('background-color','black');
If it is just a div
then you should wrap it in document.ready()
method
$(document).ready(function(){
$('#gridbox').css('background-color','black');
});
Demo
The same code that you are using is working fine for me
<html>
<head>
<script src="http://code.jquery./jquery-1.4.2.min.js"></script>
</head>
<body>
<div id="gridbox" style="background-color:red;">gridbox</div>
<script type="text/javascript">
$("#gridbox").css({'background-color':'black'});
</script>
</body>
</html>
so you should first include the jquery library, then put the javascript file after the div that you want to change the background color.
Try:
$("#gridbox").css("background-color", "black");
Edit:
Add a document ready function, this will execute the css change after the DOM
has loaded.
$(document).ready(function() {
$("#gridbox").css("background-color", "black");
});
Note: Ensure you have the jQuery JavaScript file referenced in the page.
本文标签: javascriptBackgroundcolor not changing by jQueryStack Overflow
版权声明:本文标题:javascript - Background-color not changing by jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741411205a2377245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论