[英]Jquery - Css background modification doesn't work
HTML: HTML:
<div class="rating" id = "r1"></div>
<div class="rating" id = "r2"></div>
<div class="rating" id = "r3"></div>
<div class="rating" id = "r4"></div>
<div class="rating" id = "r5"></div>
Js (using jquery 1.10.1) : Js(使用jquery 1.10.1):
<script type = "text/javascript">
$(document).ready(function(){
var rate = new Array();
rate[1] = "r1";
rate[2] = "r2";
rate[3] = "r3";
rate[4] = "r4";
rate[5] = "r5";
var r = <?php echo $rate;?>
for(var i=1; i<=r;i++){
var k = rate[i];
$('#'+k).css{('background-image': 'url(images/star_green.png)')};
}
});
</script>
Basically what i want to do using this code is to modify the background of the first x divs (number provided in my db). 基本上,我想使用此代码执行的操作是修改第一个x div(在我的数据库中提供的数字)的背景。 I know that the js variable r takes the right value...same for the k variable...the only thing that i think is not working is the part where the background is set.
我知道js变量r具有正确的值...与k变量相同...我认为唯一不起作用的是设置背景的部分。 I tested it using direct values (without the +k part) and it didn't work the either.
我使用直接值(不含+ k部分)对其进行了测试,但两者均无效。 The page is located in root and the images folder is next to it.
该页面位于根目录中,图像文件夹位于该目录的旁边。
Any suggestions? 有什么建议么?
PS: The Js script is placed on the page after the divs. PS:Js脚本位于div之后的页面上。
Your missing a semicolon here 您在这里缺少分号
var r = <?php echo $rate;?>
It should be 它应该是
var r = <?php echo $rate;?>;
Also 也
$('#'+k).css{('background-image': 'url(images/star_green.png)')};
should be 应该
$('#'+k).css({'background-image': 'url(images/star_green.png)'});
$(document).ready(function () {
var rate = new Array();
rate[1] = "r1";
rate[2] = "r2";
rate[3] = "r3";
rate[4] = "r4";
rate[5] = "r5";
var r = <? php echo $rate; ?> ;
for (var i = 1; i <= r; i++) {
var k = rate[i];
$('#' + k).css
({'background-image': 'url(images/star_green.png)'});
};
}
});
Please try this. 请尝试这个。
$('.rating').css('background-image','url(images/image.jpeg)');
$('。rating')。css('background-image','url(images / image.jpeg)');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.