调用 JavaScript 函数时出错——“找不到变量”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8957675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:08:41  来源:igfitidea点击:

Error when calling JavaScript function — "can't find variable"

javascript

提问by cole

I'm attempting to complete and exercise from the JavaScript Bible, and am having trouble getting my script to function.

我正在尝试根据 JavaScript 圣经来完成和练习,但在让我的脚本运行时遇到了麻烦。

The assignment is to create a page that allows users to query a planet's name, and, via a script that matches the planet's name with its data stored in the associate arrays, call up its distance and diameter information.

任务是创建一个页面,允许用户查询行星的名称,并通过将行星名称与其存储在关联数组中的数据进行匹配的脚本,调用其距离和直径信息。

I'm attempting to call the function 'getPlanetInfo' via a button (onclick='getPlanetInfo()'). However, my error console reports that it cannot find a variable named 'getPlanetInfo' when I attempt to run it.

我正在尝试通过按钮(onclick='getPlanetInfo()')调用函数“getPlanetInfo”。但是,我的错误控制台报告说,当我尝试运行它时,它找不到名为“getPlanetInfo”的变量。

I've attached both my JS and HTML code below. Any idea as to why my function isn't being properly called would be hugely appreciated.

我在下面附上了我的 JS 和 HTML 代码。任何关于为什么我的函数没有被正确调用的想法将不胜感激。

HTML:

HTML:

<!DOCTYPE html>
<html>
<head>
    ...
    <script type="text/javascript" src="planets.js"></script>
</head>
<body>
        <h1>Check a planet's distance from the sun and its diameter!</h1>
        <form>
            <input type="text" name="entry" id="entry">
            <input type="button" value="Check it!" onClick="getPlanetInfo()">
        </form>
</body>
</html>

JS:

JS:

var planetNames = new Array(4);
planetNames[0] = "Mercury";
planetNames[1] = "Venus";
planetNames[2] = "Earth";
planetNames[3] = "Mars";

var planetDistances = new Array(4);
planetDistances[0] = "36 million miles";
planetDistances[1] = "67 million miles";
planetDistances[2] = "93 million miles";
planetDistances[3] = "141 million miles";

var planetDiameters = new Array(4);
planetDiameters[0] = "3,100 miles";
planetDiameters[1] = "7,700 miles";
planetDiameters[2] = "7,920 miles";
planetDiameters[3] = "4,200 miles";

function getPlanetInfo()
{
var selectedPlanet = document.getElementById("entry").value;
for (var i = 0; i < planetNames.length; i++)
{
    if (planetNames[i] == selectedPlanet)
    {
        break;
    }
}

if (i < planetNames.length)
{
    alert(selectedPlanet + " is " + planetDistances[i] " in distance from the Sun and " + planetDiameters[i] + "in diameter.")
}

else
{
    alert("Sorry, " + selectedPlanet + " isn't in the database.");
}
}

采纳答案by Marcelo Diniz

This line:

这一行:

alert(selectedPlanet + " is " + planetDistances[i] " in distance from the Sun and " + planetDiameters[i] + "in diameter.")

is missing a +sign after planetDistances[i], so the function has a syntax error and is not created, and naturally it's not found when called.

+后面缺少一个符号planetDistances[i],所以函数有语法错误,没有创建,调用时自然找不到。

http://www.jsfiddle.nethelps you create a reproducible case that we can all see, use it when you need to ask js questions.

http://www.jsfiddle.net帮助你创建一个我们都可以看到的可重现的案例,当你需要问js问题时使用它。

回答by James McLaughlin

You're missing a +- this:

你缺少一个+- 这个:

alert(selectedPlanet + " is " + planetDistances[i] " in distance from the Sun and " + planetDiameters[i] + "in diameter.")

should be

应该

alert(selectedPlanet + " is " + planetDistances[i] + " in distance from the Sun and " + planetDiameters[i] + "in diameter.")

You should use something like Firebug to catch syntax errors when loading your script.

在加载脚本时,您应该使用 Firebug 之类的工具来捕获语法错误。