开发者

Jquery background image css change not working

开发者 https://www.devze.com 2023-02-08 06:35 出处:网络
I am trying to change the b开发者_StackOverflow中文版ackground image of an element through jquery like this;

I am trying to change the b开发者_StackOverflow中文版ackground image of an element through jquery like this;

$j(this).css('background-image','url(images/client_box_grad.gif)');

However when it renders it seems to drop speech marks around the the url, eg

$j(this).css('background-image','url("images/client_box_grad.gif")');

And this means the image isn't visible - if I remove the speech marks in Firebug then the image shows up.

Any ideas ?


The double quotes are not necessary:

$(this).css('background-image', 'url(/images/client_box_grad.gif)');

You should make sure that you have specified a valid image url. Here's a demo.


This is what I had to do to get it to work:

$(function(){
$('body').css({backgroundImage : 'url(/media/bill.jpg)'});
});


Try these:

$(this).css('background-image','url(images/client_box_grad.gif)');

// OR
$(this).css('background', 'url("images/client_box_grad.gif")');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号