在div中更改背景

时间:2012-11-06 06:29:19

标签: html

我从他们更改背景图片的网站获得此代码。我想使用旧的背景图片,但不知道如何。来自网站的原始代码:

  <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")';

我的问题是这样 - 我如何修改我的GM脚本以便它应用于特定的div?或者我如何只更改现有代码中的一个字符(377714到377712)

2 个答案:

答案 0 :(得分:0)

只要您要使用的图像与 texture_bg_tile_big_city.jpg 位于同一文件夹中,您就可以更改该文件名。

如果你想修改你的脚本只在一个div上设置背景图像,你必须在div中添加一个ID(如果你还没有)。

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

答案 1 :(得分:0)

使用jQuery更改背景图像CSS有两种不同的方法

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

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

仔细注意区别。在第二种情况下,您可以使用传统的CSS和多个CSS属性串联起来。

试试这个:

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

希望它有所帮助!