【问题标题】:Javascript to have a different background image of a div on refreshJavascript在刷新时具有不同的div背景图像
【发布时间】:2013-01-10 09:52:28
【问题描述】:

请你告诉我为什么这个 JavaScript 在刷新时有一个不同的背景图像 div 不起作用:

<head>
<script type="text/javascript">
    window.onload = function () {    
        var thediv = document.getElementById("top");
        var imgarray = new Array("banner2.jpg", "banner3.jpg");
        var spot = Math.floor(Math.random()* imgarry.length);
        thediv.style.background = "url(images/header/banner/"+imgarray[spot]+")";
    }
</script>
</head>

该网站可以在http://physoc.org.uk查看

谢谢。

【问题讨论】:

  • 正确的代码格式肯定有帮助。您在声明 spot 变量的那一行将“imgarray”拼错为“imgarry”。

标签: css refresh background-image


【解决方案1】:

我的 Javascript 控制台说:

Timestamp: 26.01.2013 17:00:19
Error: ReferenceError: imgarry is not defined
Source File: http://www.physoc.org.uk/
Line: 84

您的代码中有错字。你在某处使用了“imgarry”而不是“imgarray”。

按 CTRL+SHIFT+J 打开控制台并亲自查看,适用于 Firefox 和 Chrome。调试Javascript代码非常有用。

【讨论】:

    【解决方案2】:

    更改此行。有一个typo

    来自

    var spot = Math.floor(Math.random() * imgarry.length);  
    

    var spot = Math.floor(Math.random() * imgarray.length);  
    

    【讨论】:

    • 谢谢,我早该发现的
    猜你喜欢
    • 2015-01-17
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 2017-01-02
    • 2012-06-30
    • 1970-01-01
    相关资源
    最近更新 更多