无法调用JavaScript函数 - ReferenceError:找不到变量

时间:2011-12-22 19:04:22

标签: javascript function variables

这实际上是一个Adobe AIR应用程序,但由于我遇到的问题是使用非常基本的JavaScript,我想我应该把它放在这里。

以下是代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>KM Ciclo de Servicio</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/ciclo.css" rel="stylesheet" type="text/css" />
<script src="script/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function changefield()
{
    #('#ciclo').append('ASD');
}
</script>
</head>
<body oncontextmenu="return false;">
<div id="topMenu">
    <button class="menuItem" id="new" title="Nuevo"><img src="images/new.png" alt="Nuevo"/></button>
    <button class="menuItem" id="save" title="Guardar"><img src="images/save.png" alt="Guardar"/></button>
    <button class="menuItem" id="open" title="Abrir"><img src="images/open.png" alt="Abrir"/></button>
    <input type="text" value="Momento cambiado" onkeypress="changefield()"/>
        <div style="display: inline-block; float: right;">
        Nuevo:
        <button class="menuItem" id="momento">Momento</button>
        <button class="menuItem" id="atributo">Atributo</button>
    </div>
</div>
<div id="main" align="center">
    <div id="ciclo"></div>
</div>
<script src="script/init.js" type="text/javascript"></script>

</body>
</html>    

这是我试图打电话的功能:

<script type="text/javascript">
function changefield()
{
    #('#ciclo').append('ASD');
}
</script>

但是,我的终端告诉我line 20上有一个错误,即:

<input type="text" value="Momento cambiado" onkeypress="changefield()"/>

错误说:

  

ReferenceError:找不到变量:changefield
  app上的onkeypress:/index.html:19

我已经检查了一些关于JavaScript函数的教程以确保(包括w3schools中的那个),并且似乎没有任何错误。我正在调用函数,这些教程是如何建议我这样做的。

有关正在发生的事情的任何想法?

3 个答案:

答案 0 :(得分:5)

正确的语法是:

function changefield()
{
    $('#ciclo').append('ASD');
}

这是一个javascript语法错误:

#('#ciclo').append('ASD');

导致无法定义更改字段功能。

答案 1 :(得分:3)

问题可能是jQuery创建了$变量,而不是#

function changefield()
{
    $('#ciclo').append('ASD');
}

答案 2 :(得分:3)

因为#应为$

function changefield() {
    $('#ciclo').append('ASD');
}

SyntaxError导致changefield函数无法创建,因此内联onkeypress无法找到它。