当前位置: 动力学知识库 > 问答 > 编程问答 >

html - Change background in div

问题描述:

I have this code from a website in which they changed the background image. I would like to use the old background image, but not sure how to. Original code from website:

 <div style="position: absolute; top: -696px; left: -964px; width: 2944px; height: 1840px; transform: scale(1); transform-origin: 0px 0px; background-image: url("http://prodcdngame.lordofultima.com/cdn/377714/resource/webfrontend/townlayer/texture_bg_tile_big_city.jpg");">

Original code XPath = /html/body/div[3]/div/div/div/div/div/div/div/div/div/div

so far I have this, but it only changes the background of the page during loading bar:

document.body.element.style.background = 'url("http://prodcdngame.lordofultima.com/cdn/377712/resource/webfrontend/townlayer/texture_bg_tile_big_city.jpg")';

My question is this - how do I rework my GM script so that it is applied to the specific div? OR How can i change just one character in the existing code (377714 to 377712)

网友答案:

As long as the image you want to use is in the same folder as texture_bg_tile_big_city.jpg you can just change that file name.

If you want to modify your script to only set the background image on one div you will have to add an ID to the div (if you don't already have one).

document.getElementById(NameOfID).style.backgroundImage = 'url("http://prodcdngame.lordofultima.com/cdn/377712/resource/webfrontend/townlayer/texture_bg_tile_big_city.jpg")';
网友答案:

There are two different ways to change a background image CSS with jQuery

1 $('selector').css('backgroundImage','url(images/example.jpg)');

2 $('selector').css({'background-image':'url(images/example.jpg)'});

Look carefully to note the differences. In the second, you can use conventional CSS and string multiple CSS properties together.

Try this:

document.getElementById('a').style.backgroundImage="url(images/img.jpg)"; // specify the image path here

Hope it helps!

分享给朋友:
您可能感兴趣的文章:
随机阅读: