Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我调用JavaScript函数是否正确?_Javascript_Function - Fatal编程技术网

我调用JavaScript函数是否正确?

我调用JavaScript函数是否正确?,javascript,function,Javascript,Function,我是Javascript新手 我正在制作我的第一个冒险游戏 我用onClick测试了以下代码,效果很好: // JavaScript Document function changeColour() { if (document.getElementById('colourTest').style.backgroundColor='yellow') { document.getElementById('colourTest').style.background

我是Javascript新手

我正在制作我的第一个冒险游戏

我用onClick测试了以下代码,效果很好:

// JavaScript Document
function changeColour() 
{
    if (document.getElementById('colourTest').style.backgroundColor='yellow') 
    {
        document.getElementById('colourTest').style.backgroundColor='red';
    }
    else
    {
        document.getElementByID('colourTest').style.backgroundColor='yellow';
    }
}

var direction;

direction = prompt("Which direction would you like to go ?");

if ( direction == "North" )
{
    changeColour();
}
else
{
    console.log("You can't go in that direction ?");    
}
这是HTML:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script language="javascript" type="text/javascript" src="Scarry_Adventure_Game.js"></script>
<link rel="stylesheet" href="Scarry_Adventure_Game.css" type="text/css">
</head>

<body>
    <div id="colourTest">
    </div>
</body>
</html>
我不明白为什么用户的输入方向不允许图像更改为image2.jpg,而用户输入的是单词North

JS是否真的可以从html中捕获文本输入,然后将其与函数中的变量一起使用


此外,在这个版本中,DOM似乎没有加载,因为没有图像可供查看。

我立即看到的两个错误(可能更多)是

你是在分配而不是比较。使用
==
进行比较(就像您在其他地方所做的那样)。而且

JavaScript区分大小写。函数名应该是
getElementById
(与其他地方一样)


在本例中,存在逻辑错误和语法错误。后者通常可以通过查看浏览器调试工具中的JavaScript控制台来发现。如果它试图执行那行代码,你会看到一个错误。另一方面,逻辑错误可能更难确定。对于那些您希望熟悉浏览器调试工具中的调试器的用户


您可以单击JavaScript代码的特定行来设置一个“断点”,代码的执行将在此处暂停。暂停后,您可以检查变量的运行时值,逐行检查执行以检查其行为,等等。这就是验证代码是否正在执行您希望它执行的操作的方式。

观察您的情况-您的getElementBy ID()在一行中如果(document.getElementById('colorTest').style.backgroundColor='yellow'),这是一个分配,使用“==”来测试相等。您如何从用户输入中捕获var方向?如果您没有任何特定问题,但希望获得有关代码的反馈,可能是一个更好的地方询问谢谢大家,我已修复错误:DHi David,谢谢。我找不到我的函数不起作用的原因。我在控制台中有一个错误:Uncaught TypeError:无法读取null Scarry_Adventure_Game.js:4 ChangeColor Scarry_Adventure_Game.js:4(匿名函数)@user3683006:经验性。抛出该错误的代码行是什么?该行执行时的值是什么?该错误意味着您试图调用
null
对象上的
.style
属性,这意味着您的一行代码没有找到要查找的HTML元素。谢谢Dave。我现在看到我需要编写代码来更改链接的css。回到学习JS:SI编辑了我的原始问题,并更新了:D
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script  type="text/javascript" src="Scarry_Adventure_Game.js"></script>
<script  type="text/javascript" ></script>
<link rel="stylesheet" href="Scarry_Adventure_Game.css" type="text/css">

</head>

<body onload="load();">

        <img id="myimg" alt="My Image" src="images/image1.jpg" />
        <form>
            <input name="heading" type="text" id="which" value="" />    
        </form>

</body>
</html>
   // JavaScript Document

        var img = new Image();
        img.src = "images/image1.jpg";



function whichImage(b)
{
    var image = document.getElementById("myimg");

    if (b == "North")
    {
        image.src = "images/image2.jpg";
    }
    else
    {
        image.src = "images/image1.jpg";
    }
}

function whichDirection (x) 
{
    if (x == "North" || x == "South" || x == "East" || x == "West" ) 
    {
        document.write("You choose to go " + direction);
    }
    else
    {
        document.write("You can't go in that direction");
    }
}

function load()
{
    var direction = document.getElementById('which').value;

    whichDirection(direction);
    whichImage(direction);


}
if (document.getElementById('colourTest').style.backgroundColor='yellow')
document.getElementByID('colourTest').style.backgroundColor='yellow';