nik*_*hil 34 javascript jquery getelementbyid
我正在学习jQuery并且正在学习一个教程,一个非常奇怪的错误困扰着我.这是我的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>
Run Code Online (Sandbox Code Playgroud)
和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 = "";
};
Run Code Online (Sandbox Code Playgroud)
当我添加元素时通过点击返回键,没有问题.但是,当我单击"提交"按钮时,firebug会显示此错误
document.getElementByID is not a function
[Break On This Error] add_task(document.getElementByID("task-text"),evt);
task-list.js (line 11)
Run Code Online (Sandbox Code Playgroud)
我试着用jQuery替换它
$("#task-text")
Run Code Online (Sandbox Code Playgroud)
这次错误是:
$("<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
Run Code Online (Sandbox Code Playgroud)
我一直试图调试这个已经有一段时间,但我只是不明白.这可能是我犯过的一个非常愚蠢的错误.任何帮助都非常感谢.
编辑:我正在使用jQuery 1.6.1
Buh*_*ndi 99
它是,document.getElementById()
而不是document.getElementByID()
.检查外壳Id
.
3zz*_*zzy 18
就我而言,我在一个element
而不是上使用它document
,并且根据 MDN:
与其他一些元素查找方法如 Document.querySelector() 和 Document.querySelectorAll() 不同,getElementById() 仅作为全局文档对象的方法可用,而不能作为 DOM 中所有元素对象的方法。因为 ID 值在整个文档中必须是唯一的,所以不需要函数的“本地”版本。
归档时间: |
|
查看次数: |
117320 次 |
最近记录: |