首页 > 解决方案 > 另一个“未捕获的语法错误:意外的标识符”

问题描述

拜托,我在使用 sntax 时遇到问题,我不知道为什么。我的脚本是

var popup = document.getElementById("mypopup");
var span = document.getElementsByClassName("close-btn")[0];
var sticker = document.getElementsByClassName("sticker")[0];

window.onload = function() {
  popup.style.display = "inline-block";
  sticker.style.display = "none";
}

span.onclick = function() {
  popup.style.display = "none";
  sticker.style.display = "inline-block";
}

window.onclick = function(event) {
  if (event.target == popup) {
    popup.style.display = "none";
    sticker.style.display = "block";
  }
}

似乎错误就在这条线上

span.onclick = function() {

控制台消息只是“Uncaught SyntaxError: Unexpected identifier”并且只出现在生产环境中。

希望你能救我!

标签: javascriptsyntax-errorproduction-environment

解决方案


您需要像这样使用 addEventListener span.addEventListener("onclick", yourFunction) 否则它不起作用


推荐阅读