2011-06-20 83 views
19

我正在学习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

+6

题外话:如果你在学习jQuery,FYI,你很少会想要使用'document.getElementById'。 jQuery等价物是带有ID选择器的[$'(或'jQuery')函数](http://api.jquery.com/jQuery/)。 jQuery实际上可以在一些浏览器的'getElementById'中解决错误,所以如果你使用库,最好使用它。 :-)(不是说你不应该知道getElementById是什么) –

+0

如果你使用的是jQuery,那么你为什么使用经典的javascript语法,即getElementById。 YOu应该使用$('#id')语法。 –

回答

76

这是document.getElementById()而不是document.getElementByID()。检查外壳Id

+1

对我来说它是'Document.getElementById()' – Anwar

16

这是getElementById()

注意小写dId

2

如果您愿意,我已经修改了您的脚本以使用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($("#task-text"),evt); 
    }); 
}); 

function add_task(textBox, evt){ 
    evt.preventDefault(); 
    var taskText = textBox.val(); 
    $("<li />").text(taskText).appendTo("#tasks"); 
    textBox.val(""); 
}; 
0

有几件事情不对的,你可以在其他职位看,但你得到的是错误的原因是因为你的名字你表单的getElementById。所以document.getElementById现在指向你的表单而不是JavaScript提供的默认方法。看到我的小提琴进行工作演示https://jsfiddle.net/jemartin80/nhjehwqk/

function checkValues() 
{ 
    var isFormValid, form_fname; 

    isFormValid = true; 
    form_fname = document.getElementById("fname"); 
    if (form_fname.value === "") 
    { 
     isFormValid = false; 
    } 
    isFormValid || alert("I am indicating that there is something wrong with your input.") 

    return isFormValid; 
} 
相关问题