这个javascript有什么问题?

时间:2011-03-07 18:31:05

标签: javascript

我正在使用javascript根据时间切换表格。但我在DW中遇到错误。

       <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 个答案:

答案 0 :(得分:3)

  1. JavaScript不支持多行字符串。
  2. 您在字符串中有未转义的单引号(以单引号分隔)。
    例如,您必须更改:

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

    为:

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

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

      

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

答案 1 :(得分: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你的链接时自己更改状态栏,这真的会让一些人生气,例如我。

答案 2 :(得分:0)

你是混合单引号和双引号,有错误,你需要逃避它们。