SyntaxError:期望的表达式,在字符2处获得'}'

时间:2018-07-14 11:49:38

标签: javascript html typeerror

我目前正在使用自己的自定义控制台处理Cookie Clicker。它应该只是一个Cookie Clicker克隆。一家商店或其他我还没有添加的东西。 问题是,当您运行代码时,会出现一个错误

expected expression, got '}' [More Information] index.html:2

(完全从firefox复制) 奇怪的是,获得的'}'位于标签中第二个使用的字符处。

如何在代码中获得“}”?

代码如下:

//init
var clicks = 0;
var button = document.getElementById("clickme");
var consoleMsgs = 0;
var myConsole = document.getElementById('console');
consoleLog("Button clicker initiated!");

function buttonClick() {
  clicks++;
  consoleLog('You have ' + clicks + ' clicks!');
  button.value = 'Clicks: ' + clicks;
}

function consoleLog(t) {
  consoleMsgs++
  myConsole.children[1].innerHTML = '<p style="margin-bottom: 0;" id=' + consoleMsgs + '>' + t + '</p>' + myConsole.children[1].innerHTML;
}
input {
  border-style: solid;
  border-color: black;
  border-width: thick;
  border-radius: 3rem;
  background: #232328;
  margin-bottom: 0.2rem;
  color: #d3dde6;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Romgl</title>

  <!-- Bootstrap Dependencies -->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
  <!-- Bootstrap -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>

</head>

<body>

  <div id="content" style="font-size: 1.5rem; margin: 0.7ex">
    <div>
      <input id="clickme" value="Clicks: 0" style="display: block; width: 100%; height: 100px;" onclick="buttonClick();" type="submit">

      <input value="clear console" style="margin-right: 10px;" onclick="myConsole.children[1].innerHTML = " ";=" " consolemsgs="0; " consolelog('console=" " cleared!');"="" type="submit">

      <div id="console" style="font-size: 1.3rem;">
        <h1>Console</h1>
        <div class="consoleText">
        </div>
      </div>

    </div>

  </div>
  </div>



</body>

</html>

请帮助我!

1 个答案:

答案 0 :(得分:0)

确实应该避免将JS放入HTML输入中。

您可以按照以下方式进行操作:

//init
var clicks = 0;
var button = document.getElementById("clickme");
var consoleMsgs = 0;
var myConsole = document.getElementById('console');
consoleLog("Button clicker initiated!");

document.getElementById("clickme").addEventListener('click', function() {
  clicks++;
  consoleLog('You have ' + clicks + ' clicks!');
  button.value = 'Clicks: ' + clicks;
});

document.getElementById("clicktoclear").addEventListener('click', function(){
  myConsole.children[1].innerHTML='';
  consoleMsgs=0;
  consoleLog('cleared!');
});

function consoleLog(t) {
  consoleMsgs++
  myConsole.children[1].innerHTML = '<p style="margin-bottom: 0;" id=' + consoleMsgs + '>' + t + '</p>' + myConsole.children[1].innerHTML;
}
input {
  border-style: solid;
  border-color: black;
  border-width: thick;
  border-radius: 3rem;
  background: #232328;
  margin-bottom: 0.2rem;
  color: #d3dde6;
}
<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <title>Romgl</title>

    <!-- Bootstrap Dependencies -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>

  </head>

  <body>

    <div id="content" style="font-size: 1.5rem; margin: 0.7ex">
      <div>
        <input id="clickme" value="Clicks: 0" style="display: block; width: 100%; height: 100px;"  type="submit">

        <input id="clicktoclear" value="clear console" style="margin-right: 10px;" type="submit">

        <div id="console" style="font-size: 1.3rem;">
          <h1>Console</h1>
          <div class="consoleText">
          </div>
        </div>

      </div>

    </div>
    <!-- Remove the extra </div> -->

  </body>

</html>

我刚刚将输入中包含的JS移到了实际的JS脚本,并更正了代码以清除控制台。 另外,您删除了HTML中的多余内容。

编辑:我刚刚删除了jQuery,以切换到纯JS以匹配原始代码。