JavaScript 抛出未捕获的语法错误:意外标记(来自 switch 语句的案例)

维斯塔

我还是 JS 的新手,并尝试实现 switch 语句来更改 HTML 按钮颜色。然而调试控制台抛出

未捕获的语法错误 - 意外的令牌情况。

我看过几个语法示例和类似的 stackoverflow 问题,但似乎我做的其他一切都是正确的;低效的 if-else 语句运行良好。有没有人看到问题?

// main.js

var counter = 0;
var pix = 3;

function addFunction() {
  document.getElementById("count").innerHTML = counter += 1;
}

function subtractFunction() {
  if (counter > 0) {
    document.getElementById("count").innerHTML = counter -= 1;
  } else {
    alert("Value cannot get negative!")
  }
}

function resetFunction() {
  document.getElementById("count").innerHTML = counter = 0;
}

function changeFunction() {
  pix += 1;
  document.getElementById("changebutton").style.padding = pix + "px " + pix + "px " + pix + "px " + pix + "px"
}

function weirdFunction() {
  var bgcolor = document.getElementById("weirdbutton");

  switch (bgcolor.style.backgroundColor) {
    case "white":
      bgcolor.style.backgroundColor = "green";
      break;
    case "green":
      bgcolor.style.backgroundColor = "blue";
      break;
    case "blue":
      bgcolor.style.backgroundColor = "red";
      break;
    case "red":
      bgcolor.style.backgroundColor = "white";
      break;
  }
}
.cardblock {
  position: center;
  text-align: center;
  background-color: white;
  padding: 0px 0px 15px 15px;
  border-radius: 30px;
  margin: 10px 100px;
}

.card {
  padding: 10px 10px 0px 0px;
  border-radius: 30px;
}

#count {
  background-color: lightgrey;
  padding: 10px;
  width: 10%;
  margin: 0 auto;
  font-size: 150%;
  border-radius: 10px;
}

button {
  width: auto;
  margin: 0 auto;
  color: black;
  border-radius: 3px;
  border-style: groove;
  padding: 2px 2px 2px 2px;
}

#addbutton {
  background-color: lightgreen;
}

#subbutton {
  background-color: pink;
}

#reset-button {
  background-color: red;
}

#changebutton {
  background-color: lightblue;
  color: black;
}

html {
  background-color: lightgray;
}
<!DOCTYPE html>
<html>

<head>
  <title id="title">Hey y'all</title>
  <script src="/main.js"></script>
  <link type="text/css" rel="stylesheet" href="index.css">
</head>

<body>
  <div class="cardblock">
    <div class="card">
      <h3>People count:</h3>
    </div>
    <div class="card">
      <h4 id="count">0</h4>
    </div>
    <div class="card">
      <button id="addbutton" onclick="JavaScript:addFunction()">Add</button>
      <button id="subbutton" onclick="JavaScript:subtractFunction()">Subtract</button>
    </div>
    <div class="card">
      <button id="reset-button" onclick="JavaScript:resetFunction()">Reset</button>
    </div>
  </div>
  <div class="cardblock" id="playground">
    <div class="card">
      <h3>Let's attempt to change the HTML elements</h3>
      <i>First, write some cr*p</i><br>
      <b>Then, make some buttons to change HTML elements or CSS props</b>
    </div>
    <div class="card">
      <button id="changebutton" onclick="JavaScript:changeFunction()">Change</button>
      <button id="weirdbutton" onclick="JavaScript:weirdFunction()">Weird</button>
    </div>
  </div>
</body>

</html>

我还没有格式化/美化一切,抱歉。

TechySharnav

elem.style.property指元素的内联样式。您需要添加内联样式switch才能工作。

var counter = 0;
var pix = 3;

function addFunction() {
  document.getElementById("count").innerHTML = counter += 1;
}

function subtractFunction() {
  if (counter > 0) {
    document.getElementById("count").innerHTML = counter -= 1;
  } else {
    alert("Value cannot get negative!")
  }
}

function resetFunction() {
  document.getElementById("count").innerHTML = counter = 0;
}

function changeFunction() {
  pix += 1;
  document.getElementById("changebutton").style.padding = pix + "px " + pix + "px " + pix + "px " + pix + "px"
}

function weirdFunction() {
  var bgcolor = document.getElementById("weirdbutton");

  switch (bgcolor.style.backgroundColor) {
    case "white":
      bgcolor.style.backgroundColor = "green";
      break;
    case "green":
      bgcolor.style.backgroundColor = "blue";
      break;
    case "blue":
      bgcolor.style.backgroundColor = "red";
      break;
    case "red":
      bgcolor.style.backgroundColor = "white";
      break;
  }
}
.cardblock {
  position: center;
  text-align: center;
  background-color: white;
  padding: 0px 0px 15px 15px;
  border-radius: 30px;
  margin: 10px 100px;
}

.card {
  padding: 10px 10px 0px 0px;
  border-radius: 30px;
}

#count {
  background-color: lightgrey;
  padding: 10px;
  width: 10%;
  margin: 0 auto;
  font-size: 150%;
  border-radius: 10px;
}

button {
  width: auto;
  margin: 0 auto;
  color: black;
  border-radius: 3px;
  border-style: groove;
  padding: 2px 2px 2px 2px;
}

#addbutton {
  background-color: lightgreen;
}

#subbutton {
  background-color: pink;
}

#reset-button {
  background-color: red;
}

#changebutton {
  background-color: lightblue;
  color: black;
}

html {
  background-color: lightgray;
}
<!DOCTYPE html>
<html>

<head>
  <title id="title">Hey y'all</title>
  <script src="/main.js"></script>
  <link type="text/css" rel="stylesheet" href="index.css">
</head>

<body>
  <div class="cardblock">
    <div class="card">
      <h3>People count:</h3>
    </div>
    <div class="card">
      <h4 id="count">0</h4>
    </div>
    <div class="card">
      <button id="addbutton" onclick="addFunction()">Add</button>
      <button id="subbutton" onclick="subtractFunction()">Subtract</button>
    </div>
    <div class="card">
      <button id="reset-button" onclick="resetFunction()">Reset</button>
    </div>
  </div>
  <div class="cardblock" id="playground">
    <div class="card">
      <h3>Let's attempt to change the HTML elements</h3>
      <i>First, write some cr*p</i><br>
      <b>Then, make some buttons to change HTML elements or CSS props</b>
    </div>
    <div class="card">
      <button id="changebutton" onclick="changeFunction()">Change</button>
      <button style="background-color: red" id="weirdbutton" onclick="weirdFunction()">Weird</button>
    </div>
  </div>
</body>

</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

JavaScript 错误 - 未捕获的语法错误:意外标记 {

JavaScript switch语句中的语法错误

JavaScript Switch语句-单个案例中的同义词

JavaScript Switch语句-可以使用案例值吗?

PHP 回显日期在 javascript 中。未捕获的语法错误:意外标记:标识符

未捕获的语法错误:“意外的令牌)”| JavaScript 中的 if else 语句

未捕获的语法错误:意外的标记“。” Javascript 画布

Javascript反应未捕获的SyntaxError:意外的标记“ <”或语法错误

javascript switch语句未触发

JavaScript Switch 语句未执行

未捕获的语法错误:意外标记:javascript 中的字符串文字。我不知道出了什么问题

javascript fizzbuzz switch语句

JavaScript switch语句?

JavaScript switch 语句混淆

主义查询生成器案例语句抛出意外的语法错误

JavaScript中的if语句与switch语句

Java switch语句中的多个/重复案例

JavaScript:“未捕获的语法错误:意外的字符串”

javascript json未捕获的语法错误意外的令牌非法

错误未捕获语法错误:意外标记{

未捕获的语法错误:Chrome 中的意外标记 {

JavaScript中的嵌套switch语句

switch语句滚动值(JavaScript)

在 JavaScript 中获取错误的 order switch case 语句

案例语句语法MySQL

Javascript 类未捕获 SyntaxError:意外标记;性别渲染

Switch语句中的错误

switch case 语句中的语法错误 react-redux

有switch语句的默认情况下调用先前的案例?