2011-03-07 91 views
1

我使用JavaScript来根据时间切换表。但是我在DW中遇到错误。这个javascript有什么问题?

 <script type="text/JavaScript"> 
<!-- 
function changeWebsite() { 
var currentTime = new Date().getHours();  

if (7 <= currentTime&&currentTime < 18) { 
     document.write(' <table id="Table_01" width="200" height="400" border="0" cellpadding="0" cellspacing="0"> 
    <tr> 
     <td> 
      <img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_VC01.png" width="200" height="45" alt=""></td> 
    </tr> 
    <tr> 
     <td> 
      <a href="http://www.itsnotch.com" 
       onmouseover="window.status='Visit My Biography Website'; return true;" 
       onmouseout="window.status=''; return true;"> 
       <img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_VC02.png" width="200" height="75" border="0" alt="ItsNotch.com"></a></td> 
    </tr> 
    <tr> 
     <td> 
      <a href="http://www.notchtheguru.com" 
       onmouseover="window.status='My Tumblr'; return true;" 
       onmouseout="window.status=''; return true;"> 
       <img src="http://itsnotch.com/tumblr/images/NotchTheGuru.comVC.png" width="200" height="119" border="0" alt="Tumblr"></a></td> 
    </tr> 
    <tr> 
     <td> 
      <a href="http://www.bignotch.com" 
       onmouseover="window.status='Welcome to My Music Website'; return true;" 
       onmouseout="window.status=''; return true;"> 
       <img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_VC04.png" width="200" height="161" border="0" alt="NotchTheGuru.com"></a></td> 
    </tr> 
</table> 
<br> 
'); 
     } 

     else { 
     document.write(' <table id="Table_01" width="200" height="400" border="0" cellpadding="0" cellspacing="0"> 
    <tr> 
     <td> 
      <img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_01.png" width="200" height="45" alt=""></td> 
    </tr> 
    <tr> 
     <td> 
      <a href="http://www.itsnotch.com" 
       onmouseover="window.status='Visit My Biography Website'; return true;" 
       onmouseout="window.status=''; return true;"> 
       <img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_02.png" width="200" height="75" border="0" alt="ItsNotch.com"></a></td> 
    </tr> 
    <tr> 
     <td> 
      <a href="http://www.notchtheguru.com" 
       onmouseover="window.status='My Tumblr'; return true;" 
       onmouseout="window.status=''; return true;"> 
       <img src="http://itsnotch.com/tumblr/images/NotchTheGuru.com.png" width="200" height="119" border="0" alt="Tumblr"></a></td> 
    </tr> 
    <tr> 
     <td> 
      <a href="http://www.bignotch.com" 
       onmouseover="window.status='Welcome to My Music Website'; return true;" 
       onmouseout="window.status=''; return true;"> 
       <img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_04.png" width="200" height="161" border="0" alt="NotchTheGuru.com"></a></td> 
    </tr> 
</table> 
<br> 
'); 
     } 

} 

changeWebsite(); 
--> 

错误是在第7行了 'document.write('

+3

什么是错误? – kjy112 2011-03-07 18:32:23

+0

只有按下ENTER键,才能在JavaScript字符串中分行。 – 2011-03-07 18:38:18

+0

DW = dreamweaver? – 2011-03-07 18:39:57

回答

0

你混合单引号和双引号,有错误,你需要躲避他们的。

3
  1. JavaScript不支持多行字符串
  2. 您在字符串内部有非转义的单引号(这是delim用单引号引用)。
    例如,你必须变化

    onmouseover="window.status='Visit My Biography Website'; 
    

    到:

    onmouseover="window.status=\'Visit My Biography Website\'; 
    

有关详细信息,通过JSLint运行代码。

警告:JSLint会伤害你的感情。

+0

+1 JSLint声明:-) – mellamokb 2011-03-07 18:40:53

1

你不应该从你的javascript写这么多HTML代码。如果你经常这样做,你的代码将很快变得不可读,这也是一个噩梦般的性能。

几乎所有你写的HTML代码是一样的,你最好做这样的事情:

HTML代码:

<table id="Table_01" width="200" height="400" border="0" cellpadding="0" cellspacing="0"> 
    <tr> 
     <td> 
      <img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_01.png" width="200" height="45" alt="" id="image1"></td> 
    </tr> 
    <tr> 
     <td> 
      <a href="http://www.itsnotch.com" 
       onmouseover="window.status='Visit My Biography Website'; return true;" 
       onmouseout="window.status=''; return true;"> 
       <img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_02.png" width="200" height="75" border="0" alt="ItsNotch.com" id="image2"></a></td> 
    </tr> 
    <tr> 
     <td> 
      <a href="http://www.notchtheguru.com" 
       onmouseover="window.status='My Tumblr'; return true;" 
       onmouseout="window.status=''; return true;"> 
       <img src="http://itsnotch.com/tumblr/images/NotchTheGuru.com.png" width="200" height="119" border="0" alt="Tumblr" id="image3"></a></td> 
    </tr> 
    <tr> 
     <td> 
      <a href="http://www.bignotch.com" 
       onmouseover="window.status='Welcome to My Music Website'; return true;" 
       onmouseout="window.status=''; return true;"> 
       <img src="http://itsnotch.com/tumblr/images/websitelist_tumblr_04.png" width="200" height="161" border="0" alt="NotchTheGuru.com" id="image4"></a></td> 
    </tr> 
</table> 
<br> 

Javascript代码:

<script type="text/JavaScript"> 
function changeWebsite() { 
var currentTime = new Date().getHours();  

if (7 <= currentTime&&currentTime < 18) { 
    var baseURL = "http://itsnotch.com/tumblr/images/"; 
    document.getElementById("image1").src = baseURL + "websitelist_tumblr_VC01.png"; 
    document.getElementById("image2").src = baseURL + "websitelist_tumblr_VC02.png"; 
    document.getElementById("image3").src = baseURL + "NotchTheGuru.comVC.png"; 
    document.getElementById("image4").src = baseURL + "websitelist_tumblr_VC04.png"; 
} 
changeWebsite() 
</script> 

作为小小的建议,你不应该改变自己的状态栏当有人mouseover你的链接,这真的有点让人失望,我例如。

+0

+1用于指出DRY代码的干净程度。我没有意识到这是一个大/复制粘贴工作。 – 2011-03-07 19:09:24