我正在学习jQuery并且正在学习一个教程,一个非常奇怪的错误让我感到困惑。 这里是我的html:document.getElementByID不是函数
<!doctype html>
<html>
<head>
<title> Simple Task List </title>
<script src="jquery.js"></script>
<script src="task-list.js"></script>
</head>
<body>
<ul id="tasks">
</ul>
<input type="text" id="task-text" />
<input type="submit" id="add-task" />
</body>
</html>
和jQuery的:
$(document).ready(function(){
//To add a task when the user hits the return key
$('#task-text').keydown(function(evt){
if(evt.keyCode == 13)
{
add_task(this, evt);
}
});
//To add a task when the user clicks on the submit button
$("#add-task").click(function(evt){
add_task(document.getElementByID("task-text"),evt);
});
});
function add_task(textBox, evt){
evt.preventDefault();
var taskText = textBox.value;
$("<li>").text(taskText).appendTo("#tasks");
textBox.value = "";
};
当我添加的元素通过敲击回车键,就没有问题。 但是当我点击提交按钮,然后萤火显示了这个错误
document.getElementByID is not a function
[Break On This Error] add_task(document.getElementByID("task-text"),evt);
task-list.js (line 11)
我试图使用jQuery,而不是与
$("#task-text")
这一次的错误是替换它:
$("<li>").text(taskText).appendTo is not a function
[Break On This Error] $("<li>").text(taskText).appendTo("#tasks");
task-list.js (line 18
which results in the following error
我一直试图调试一段时间,但我只是不明白。它可能是我犯的一个非常愚蠢的错误。任何帮助最受赞赏。
编辑: 我使用jQuery 1.6.1
题外话:如果你在学习jQuery,FYI,你很少会想要使用'document.getElementById'。 jQuery等价物是带有ID选择器的[$'(或'jQuery')函数](http://api.jquery.com/jQuery/)。 jQuery实际上可以在一些浏览器的'getElementById'中解决错误,所以如果你使用库,最好使用它。 :-)(不是说你不应该知道getElementById是什么) –
如果你使用的是jQuery,那么你为什么使用经典的javascript语法,即getElementById。 YOu应该使用$('#id')语法。 –