简体   繁体   English

如何使用localhost在页面刷新上交换背景图像?

[英]How do I swap a background image on page refresh using localhost?

I need to be able to swap a background image on page refresh. 我需要能够在页面刷新时交换背景图像。 I am attempting to do this with javascript and I'm referencing the id I have set for the body of the page. 我正在尝试使用javascript执行此操作,并且引用的是我为页面正文设置的ID。 Here is what I have come up with so far and it doesn't seem to be working: 到目前为止,这是我提出的内容,但似乎没有用:

<script type="text/javascript">
    window.onload = choosePic;

    function choosePic() {
    var myPix = new Array("/CoVPrototype/images/Homepage/skyline_bkgd_blue_fixed.jpg", "/CoVPrototype/images/AboutPage/about_bkgd.jpg", "/CoVPrototype/images/GovernmentPage/gov_bkgd.jpg", "/CoVPrototype/images/newsPage/news_bkgd.jpg", "/CoVPrototype/images/parkPage/park_bkgd.jpg", "/CoVPrototype/images/propertyPage/property_bkgd.jpg", "/CoVPrototype/images/enviroPage/enviro_bkgd.jpg", "/CoVPrototype/images/transportPage/transport_bkgd.jpg", "/CoVPrototype/images/businessPage/business_bkgd.jpg");
    var randomNum = Math.floor(Math.random() * myPix.length);
    document.getElementById("home_style").src = myPix[randomNum];
    }

The tag I'm referencing in the HTML is set like this (please note, all other HTML tags have been applied... just not shown here for the sake of brevity): 我在HTML中引用的标记是这样设置的(请注意,所有其他HTML标记都已应用...为简洁起见,此处未显示):

<body id="home_style">
</body> 

And finally, my CSS looks like this: 最后,我的CSS如下所示:

body {
        margin:0;
        padding:0;
        font-size:62.5%;
        font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
    body#home_style {
            background:no-repeat bottom left fixed;
            width:100%;
    }

Should this work? 应该行吗? What am I doing wrong?! 我究竟做错了什么?! I hope someone can help!! 我希望有人能帮帮忙!!

这应该工作:

document.getElementById("home_style").style.backgroundImage = "url(" + myPix[randomNum] + ")";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM