简单的javascript代码不起作用:var count = colors.推动(“红”、“绿色”);


<html>
    <head></head>
    <body>
        <script>
            var colors = new Array();
            var count = colors.push(“red”, “green”);
            alert(count);
        </script>
    </body>
</html>

我在firefox和IE上尝试了这个,你认为我的JavaScript版本需要更新吗?

您需要使用真正的引号,"',例如:

var count = colors.push('red', 'green'); 

您使用的引号字符是非法的,并且显示JavaScript错误SyntaxError: Unexpected token ILLEGAL

演示:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>demo</title>
  <script type='text/javascript'>
      var colors = new Array();
      var count = colors.push('red', 'green');
      // alert(count);
      alert(colors[0]);
  </script>
</head>
<body>
</body>
</html>

我只是复制粘贴你的代码在jsFiddle,它看起来像你的引号是非法字符(从word/web复制粘贴?)。

最新更新