未捕获的ReferenceError:未定义swapImage

时间:2014-06-21 16:50:11

标签: javascript html

我对此很新,但这是合约。

起初我遇到了麻烦,因为我只会显示一个javascript元素。我从matt ellen得到了一些帮助,我注意到我在我的代码中有window.onload两次。我已经将它们从函数中删除,并添加了一行,其中包含了两个函数。

<head>

    <link rel="shortcut icon" href="bavafavicon"/>
    <title>Berlin Airlift Veterans Association: News</title>
    <link rel="stylesheet" type="text/css" href="main.css">

</head>

<body id="news">
    <div id="container">

        <div id="left"></div>
        <div id="leftblue"></div>
        <div id="right"></div>
        <div id="topyellow"></div>
        <div id="topred"></div>

        <div id="slider">           
            <script type="javascript"> 
                var i = 0; var path = new Array(); 
                    path[0] = "1.jpg"; 
                    path[1] = "2.jpg"; 
                    path[2] = "3.jpg"; 
                    path[3] = "4.jpg";
                    path[4] = "5.jpg";
                    path[5] = "6.jpg";
                function swapImage() 
                    { 
                        document.slide.src = path[i]; 
                        if(i < path.length - 1) i++; 
                        else i = 0; 
                        setTimeout("swapImage()",5500); 
                    } 
            </script> 
            <img name="slide"/>
        </div>

            <ul id="head_nav">
                <li><a href="index.htm" class="currentButton">NEWS</a></li>
                <li><a href="aboutbava.htm" class="button">ABOUT BAVA</a></li>
                <li><a href="history.htm" class="button">HISTORY</a></li>
                <li><a href="biographies.htm" class="button">BIOGRAPHIES</a></li>
                <li><a href="calendar.htm" class="button">CALENDAR</a></li>
                <li><a href="contact.htm" class="button">CONTACT</a></li>
                <li><a href="links.htm" class="button">LINKS</a></li>
                <li><a href="donate.htm" class="button">DONATE</a></li>
            </ul>

            <div id="headlogo">
                <img src="BAVA.png" alt="BAVA logo" width="150" height="150"/>
            </div>
            <div id="subscribe">Sign up for BAVA updates!<br>

                <img src="emailBomb.png" alt="emailBomb" id="emailBomb" width="150" height="300"/>
                <form action="demo_form.asp">
                    E-mail: <input type="email" name="userid">
                    <input type="submit" value="Submit"><br></form> 

<script type="javascript">

function GetClock(){
tzOffset = +2;

d = new Date();
dx = d.toGMTString();
dx = dx.substr(0,dx.length -3);
d.setTime(Date.parse(dx))
d.setHours(d.getHours() + tzOffset);
nday   = d.getDay();
nmonth = d.getMonth();
ndate  = d.getDate();
nyear = d.getYear();
nhour  = d.getHours();
nmin   = d.getMinutes();
if(nyear<1000) nyear=nyear+1900;

if(nmin <= 9){nmin="0"+nmin}


document.getElementById('berlinClock').innerHTML=""+(nmonth+1)+"/"+ndate+"/"+nyear+"       "+nhour+":"+nmin+"";
setTimeout("GetClock()", 1000);
}
</script> <script type="text/javascript" language="javascript">
                    window.onload = function() { swapImage(); GetClock(); };
                </script>               
<div id="berlinClock"></div>



            </div>


        </div>
    <footer>
        <div id="footer">
            <p><i>15 N. College Ave, Newton, NC 28658   |   (828) 466-3410</i></p>
        </div>
    </footer>

</body>

我不再使用外部js。

1 个答案:

答案 0 :(得分:2)

您将window.onload设置两次。它将是第二个,即GetClock,它将被运行,而不是第一个。

你需要添加一个函数调用,在你声明了这两个函数之后调用它们,然后删除其他赋值给onload。

window.onload = function() { swapImage(); GetClock(); };

这是一个fiddle来证明我的意思。

撇开;您的第一个脚本代码中的语言属性不正确。它应该是javascript而不是JavasScript/text