SyntaxError:正则表达式和白色屏幕后的无效标志

时间:2015-06-05 20:29:16

标签: javascript html arrays regex syntax-error

我无法弄清楚为什么我的网页无效。当我尝试加载它时,它只是给我一个白色的屏幕。有遗漏的代码吗?我不知道我的空间是否也很重要。这是我的代码:

<!DOCTYPE html>
<html>
<head><body>
<title>Initializing an Array</title>
<style type="text/css">
table {width:10em}
th {text-align:left}
</style>

<script language="JavaScript">
<!-- 
{//create (declare) two new arrays
var n1=new Array(5); //allocate five-element Array
var n2=new Array (); //allocate empty Array

//assign values to each element of Array n1
for ( var i = 0; i <n1.length; ++i )
n1[ i ] = i;

//create and initialize five elements in Array n2
for ( i=0; i <5; ++i )
n2[ i ] = i;

outputArray("Array n1:",n1);
outputArray("Array n2:",n2);

//output the heading followed by a two-column table
//containing subscripts and elements of "theArray"
function outputArray (heading,theArray)}
{
document.writeln("<h2>"+heading+"</h2>");
document.writeln("table border=\"1\"");
document.writeln("<thead><th>Subscripts</th>"+"<th>Value</th></thead>    <tbody>");

//output the subscript and value of each array element
for ( var i = 0; i <theArray.length; i++ )
document.writeln("<tr><td>+i+"</td><td>"+theArray[i]+"</td></tr>");

document.writeln("/tbody></table>");
} //end function outputArray
//-->

</script>

</head></body>
</html>

请帮忙!感谢。

3 个答案:

答案 0 :(得分:1)

你破坏了HTML和无效的Javascript。

<script language="JavaScript">

对于DOCTYPE html,除了<script>之外,无需为src标记指定任何属性,只有在加载外部脚本文件时才需要。并且没有属性language。您可能意味着type="text/javascript",但这是默认值,因此是多余的。

<!-- 

您不能在<script>块中包含HTML注释。您告诉浏览器<script>块中的内容是Javascript,然后您输入无效的javascript。控制台中可能会显示错误。

你的例子,整理,看起来像这样:

<script>

    // create (declare) two new arrays
    var n1 = new Array(5); //allocate five-element Array
    var n2 = new Array(); //allocate empty Array

    // assign values to each element of Array n1
    for (var i = 0; i < n1.length; ++i) {
        n1[ i ] = i;
    }

    // create and initialize five elements in Array n2
    for (i=0; i < 5; ++i) {
        n2[ i ] = i;
    }

    outputArray("Array n1:",n1);
    outputArray("Array n2:",n2);

    // output the heading followed by a two-column table
    // containing subscripts and elements of "theArray"
    function outputArray (heading, theArray) {
        var html = '<h2>' + heading + '</h2>';

        html += '<table border="1">';
        html += '<thead><th>Subscripts</th><th>Value</th></thead>';
        html += '<tbody>';

        // output the subscript and value of each array element
        for (var i = 0; i < theArray.length; i++) {
            html += '<tr><td>' + i + '</td><td>' + theArray[i] + '</td></tr>';
        }

        html += '</tbody></table>';

        var div = document.createElement('div');
        div.innerHTML = html;

        document.body.appendChild(div);
    }

</script>

答案 1 :(得分:0)

您忘记了引用"以及最后<循环中</tbody>的下一行for

document.writeln("<tr><td>"+i+"</td><td>"+theArray[i]+"</td></tr>");
                          ^
document.writeln("</tbody></table>");  
                  ^

答案 2 :(得分:0)

当然没有显示任何内容,基本HTML布局完全错误。

您写道:

<head>
  <body>
     (all the stuff)
  </head>
</body>

应该是:

<head>
   (scripts.....)
</head>
<body>
   (HTML....)
</body>