【问题标题】:SyntaxError: invalid flag after regular expression and white screenSyntaxError:正则表达式和白屏后的标志无效
【发布时间】:2015-06-05 20:29:16
【问题描述】:

我无法弄清楚为什么我的页面无法正常工作。当我尝试加载它时,它只会给我一个白屏。有没有我遗漏的代码?我也不知道我的空间是否重要。这是我的代码:

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

请帮忙!谢谢。

【问题讨论】:

  • 控制台显示什么?
  • document.writeln("&lt;tr&gt;&lt;td&gt;+i+"&lt;/td&gt;&lt;td&gt;"+theArray[i]+"&lt;/td&gt;&lt;/tr&gt;"); 这一行甚至像问题中的正则表达式一样突出显示。你忘记了"。这是一个错字。
  • 另外,那些document.writeln导致无效的HTML,因为HTML标签会自动关闭 一旦它们被写入页面。
  • &lt;head&gt;&lt;body&gt;---&lt;/head&gt;&lt;/body&gt; ???难怪什么都没有显示。应该是&lt;head&gt;------&lt;/head&gt;&lt;body&gt;-----&lt;body&gt;
  • 那我应该改用什么呢?

标签: javascript html arrays regex syntax-error


【解决方案1】:

您破坏了 HTML 和无效的 Javascript。

<script language="JavaScript">

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

<!-- 

您不能在 &lt;script&gt; 块中包含 HTML cmets。您告诉浏览器 &lt;script&gt; 块内的内容是 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>

【讨论】:

  • @Xufox 呸!一定会发生...来源是一个一团糟。为什么在地球上人们不为他们的代码感到自豪?这就像在不了解语法的情况下写小说。
【解决方案2】:

您忘记了在最后一个for 循环中的&lt;/tbody&gt; 中的" 和下一行&lt;

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

【讨论】:

    【解决方案3】:

    当然什么都没有显示,基本的 HTML 布局完全错误。

    你写的:

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

    应该是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-01
      • 2023-03-06
      • 2017-02-07
      • 1970-01-01
      相关资源
      最近更新 更多