Why am I getting the JS error in line one of my script Syntax error: invalid or unexpected token?

问题

当您按下按钮时,我正在尝试更改 index.html 页面上的图片。 这是我的 HTML:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <button id="lolcatButton">Show me the LOLCat!</button>
  <br><br>
  <img id="lolcatImage" src=https://s3.amazonaws.com/media.skillcrush.com/skillcrush/wp-content/uploads/2016/09/cat5.jpg>
  <script src="script.js"></script>
</body>

这是我的js:

var lolcatButton = document.getElementById(“lolcatButton”);    
var lolcatImage = document.getElementById(“lolcatImage”);    
var image = "https://s3.amazonaws.com/media.skillcrush.com/skillcrush/wp-content/uploads/2016/09/cat4.jpg";     

var showMeTheLolcat = function(){    
    lolcatImage.src = image;    
};     

lolcatButton.addEventListener(“click”, showMeTheLolcat);

我在 js“未捕获的语法错误:无效或意外令牌”的第 1 行收到错误。

有没有人有任何想法? 非常感谢你!

回答1
var lolcatButton = document.getElementById("lolcatButton");
回答2

您可以像这样重新组织您的代码:

<button id="lolcatButton" onclick="showMeTheLolcat()">Show me the LOLCat!</button>
  <br><br>
<img id="lolcatImage" src=https://s3.amazonaws.com/media.skillcrush.com/skillcrush/wp-content/uploads/2016/09/cat5.jpg>


<script>
function showMeTheLolcat() {
var image = "https://s3.amazonaws.com/media.skillcrush.com/skillcrush/wp-content/uploads/2016/09/cat4.jpg";
lolcatImage.src = image;
}
</script>

在 jsfiddle 上尝试一下。


更多相关内容:请点击查看