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

I'm trying to make the picture on the index.html page change when you push the button. Here's my 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> <hr>

And here's my 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); <hr>

I'm getting an error on line 1 of the js "uncaught syntaxerror: invalid or unexpected token."

Does anyone have any ideas? Thank you very much!

Answer1:

var lolcatButton = document.getElementById("lolcatButton");

Change the <strong>”</strong> to <strong>"</strong>. And the other lines.

Answer2:

You can reorganize your code like this:

<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>

Try it on jsfiddle here.

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

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