TypeError:namefunction不是函数

TypeError: namefunction is not a function

本文关键字:函数 namefunction TypeError      更新时间:2023-09-26

下面是我的代码:

HTML:

<input id="test" onkeydown="test();" type="text"/>

JS:

function pl(id){
    return id;
}
function test(){
 id = "test";
 alert(pl(id));   
}

为什么浏览器返回TypeError: test is not a function

问题中的代码没有问题,但您提供的代码的实际示例将您的函数定义封装在另一个函数中(执行onload)。

它们的作用域是其包含函数,因此不能使用onclick属性(只能接触全局变量)访问它们。

改为以程序方式绑定事件处理程序。

document.getElementById('test').addEventListener(
    "keydown",
    test
    );

例如:http://jsfiddle.net/9tFHA/2/