我正在學習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')語法。 –