SyntaxError: invalid flag after regular expression and white screen

SyntaxError: invalid flag after regular expression and white screen

我不明白为什么我的页面无法运行。当我尝试加载它时,它只是给我一个白屏。我缺少代码吗?我也不知道我的空间是否重要。这是我的代码:

<!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>

请帮忙!谢谢

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

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

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

您写道:

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

应该是:

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

你已经破坏HTML并且无效Javascript。

<script language="JavaScript">

对于 html 的 DOCTYPE,除了 src 之外,没有必要为 <script> 标签指定任何属性,只有当您加载外部脚本文件。而且没有属性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>