开发者

Change background-image with jQuery - doesn´t work

开发者 https://www.devze.com 2023-03-28 19:15 出处:网络
$(\'#areaView\').css(\'background-image\', \'url(/areaNr1.jpg)\'); I don´t know what to say, I h开发者_运维技巧ave no idea why this code doesn´t work. The old background-image disappears, but the
$('#areaView').css('background-image', 'url(/areaNr1.jpg)');

I don´t know what to say, I h开发者_运维技巧ave no idea why this code doesn´t work. The old background-image disappears, but the new one never appears.

I have checked and areaNr1.jpg exists in the same folder as the code. Allthough the function isn´t in the same folder as #areaView, but that should not matter, right?

Any ideas why this doesn´t work?

Thanks for your help!


The code I provided is in mydomain.com/jQuery/myfile.js and the image also is in that folder.

Then you mustn't prefix the URL with a / - that makes it point to the web root.

Use just areaNr1.jpg - note hoever that the URL will be relative to the HTML file, not the JS file in this case so you may need to change the path further.


You need to quote the argument to url():

$('#areaView').css('background-image', 'url("/areaNr1.jpg")');


Since the image is at mydomain.com/jQuery/areaNr1.jpg, you need to include the folder, too. If it was CSS, you could make it relative to CSS's folder, but not with JS.

$('#areaView').css('background-image', 'url("jQuery/areaNr1.jpg")');


If the html file that is running the jQuery is in the same folder as your image try:

$('#areaView').css("background", "url('areaNr1.jpg')");

If the image is in a folder it would be

$('#areaView').css("background", "url('folder/areaNr1.jpg')");

or if its in the directory above

$('#areaView').css("background", "url('../areaNr1.jpg')");

If the image is in /JQuery where is the html file?


$('#areaView').css('background-image', 'url(/jQuery/areaNr1.jpg)');

this might solve the problem

0

精彩评论

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