如何动态更改html背景

时间:2009-07-28 14:24:22

标签: javascript html

我的页面在css文件中有这样的样式:

body
{
    background: #000000 url(images/back_all.gif) repeat-x top;
    font: 12px Tahoma, Arial, Helvetica, sans-serif;
    color: #666666;
}

我有一个在window.onload之后加载的javascript,导致页面垂直扩展,因此我的页面的其余部分将变黑。 javascript代码完成后如何将背景图像再次应用到我的页面?

8 个答案:

答案 0 :(得分:9)

jQuery:

$('body').css('backgroundImage', 'url(something.gif)');


<强>非jQuery的:

指定正文ID:<body id="the_body">

document.getElementById('the_body').style.backgroundImage = "something.gif"

或约翰提到

document.body.style.backgroundImage = "something.gif";

此外,提到了一个T POPs,确保它最终结束:

<html>
<body>
..... Data ........
<script type="text/javascript">
.... Code ........
</script>
</body>
</html>

答案 1 :(得分:2)

我认为在文档末尾插入此文件可能有效:

<script type="text/javascript"> 
    var obj= document.getElementByName("body");
    obj.style.background = "#000000 url(images/myimage.gif) repeat-x top";
    // change color, image and settings
</script>

问候

答案 2 :(得分:0)

某些浏览器有一个错误(chrome,safari),它们不会正确扩展html正文背景,直到重新加载为止。根据您在javascript中实际执行的操作,此处可能没有解决方案。

答案 3 :(得分:0)

CSS样式'repeat-x'将背景设置为仅水平扩展而不是垂直扩展。从代码中删除该部分将确保您的背景在X(水平)和Y(垂直)方向上重复。

答案 4 :(得分:0)

看看:

Dynamic CSS Changes

显示了通过Javascript实现所需内容的方法。

答案 5 :(得分:0)

will give an example hope it will helpful for you.

<a class="t-hub" href="#" title="T-HUB">

 <div>
<img src="../../images/TBO_Tran_Logo.png" class="imglogocenter" alt="" style="width: 110px; height: 64px;margin: -6px 0 0;">
 </div>  </a>

You see the class **imglogocenter** we can call the class which page we have to change the image dynamically,go through that page and put in a css property changes in a loading function .Dont go and change the css property in the main page.
Here I can demonstrate 

Example:

$(function () {
       // $('#dvLoading').fadeOut(2000);
        $('.imglogocenter').css('margin','-11px 0 0');        
    });

答案 6 :(得分:0)

尝试使用Javascript,只需在脚本中形成一个数组并使用随机方法 http://suzzcodes.com/how-to-change-the-website-background-dynamically/

答案 7 :(得分:0)

好吧,如果你像我一样,而且你是javascript和jQuery的新手,你会发现这个任务非常艰巨。但你很幸运,因为我找到了解决方案。试试这个:

在执行此操作之前链接jQuery:

$(function es() {
    $('body').addClass('sec');
});

并在CSS中执行此操作:

body {
    background-Image: url(../someImage);
}
.sec {
    background-Image: url(../anotherImage);
}

这主要做的是它给身体一个第二类,而在第二秒有一个不同的背景图像作为身体所以它覆盖身体css规则//我想//我也是新来的// / p>

相关问题