document.getElementByID不是函数 [英] document.getElementByID is not a function

查看:177
本文介绍了document.getElementByID不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习jQuery,并且正在学习教程,一个非常奇怪的错误使我感到困惑. 这是我的html:

I'm learning jQuery and was following a tutorial, a very strange error has perplexed me. Here's my html :

<!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 = "";
};

当我通过按回车键添加元素时,没有问题. 但是,当我单击提交"按钮时,firebug会显示此错误

When I add elements By hitting the return key, there's no problem. But When I click the Submit Button then firebug shows this error

document.getElementByID is not a function
[Break On This Error] add_task(document.getElementByID("task-text"),evt);
task-list.js (line 11)

我尝试使用jQuery代替

I tried to use jQuery instead replacing it with

$("#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

我已经尝试调试了一段时间,但我只是不明白.这可能是我犯的一个非常愚蠢的错误.任何帮助深表感谢.

I've been trying to debug this for some time but i just don't get it. Its probably a really silly mistake that i've made. Any help is most appreciated.

我正在使用jQuery 1.6.1

Edit : I'm using jQuery 1.6.1

推荐答案

它是document.getElementById(),而不是document.getElementByID().检查外壳是否为Id.

It's document.getElementById() and not document.getElementByID(). Check the casing for Id.

这篇关于document.getElementByID不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆