简体   繁体   English

无法运行JavaScript

[英]Can't run JavaScript

I'm following tutorial cideon on JavaScript and wrote some examples in Notepad++ and saved them as something.html. 我正在关注JavaScript教程cideon并在Notepad ++中编写了一些示例并将它们保存为something.html。 The problem is when I open it with IE or Chrome, the code between <script> and </script> tags doesn't run at all. 问题是当我用IE或Chrome打开它时, <script></script>标签之间的代码根本不运行。 What is wrong with that? 这有什么问题?

<!doctype html>
<html>
<head>
    <title>Example of prompt()</title>
    <script>
        var user_name;
        user_name = prompt("What is your name?");
        document.write("welcome to my page ")
            + user_name + "!");
    </script>
</head>
</html>

There is a syntax error in document.write statement. document.write语句中存在语法错误。

Write it as follow 写如下

document.write("welcome to my page "+ user_name + "!");

有一个太多“)”

document.write("welcome to my page " + user_name + "!");

First, move the script part out of the head to the body tag. 首先,将脚本部分从头部移动到body标签。

Second, write 第二,写

document.write("welcome to my page " + user_name + "!");

in one line and remove the first closing parenthesis. 在一行中删除第一个右括号。

 <!doctype html> <html> <head> <title>Example of prompt()</title> </head> <body> <script> var user_name; user_name = prompt("What is your name?"); document.write("welcome to my page " + user_name + "!"); </script> </body> </html> 

When you open in chrome. 当你用chrome打开时。 Press F12 or Right Click > Inspect Element. 按F12或右键单击>检查元素。 Make sure you select "Console" at the top. 确保在顶部选择“控制台”。 Pay attention and try to understand what the console tells you. 注意并尝试了解控制台告诉您的内容。

In your case, this is what it says when I paste that code into my console: 在您的情况下,当我将该代码粘贴到我的控制台时,这就是它所说的:

在此输入图像描述

"Unexpected token" This means that it can't understand the script, it was expecting a ; “意外的令牌”这意味着它无法理解脚本,它正在期待一个; or a new line because you have a bracket at the end. 或者是新行,因为最后有一个括号。 Remove that and it should work. 删除它,它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM