JavaScript:需要帮助订购我的JS吗;未捕获类型错误:无法读取属性';价值';“无效”的定义;
我是一个业余JS的人,所以请善待你的投票,因为我是新手 我得到了未捕获的类型错误: 无法读取null的属性“value” 我意识到问题在于我的js的顺序和它调用DOM的顺序,但我不确定具体在哪里 有人能帮我指出正确的方向吗 这是我的密码:JavaScript:需要帮助订购我的JS吗;未捕获类型错误:无法读取属性';价值';“无效”的定义;,javascript,html,dom,typeerror,Javascript,Html,Dom,Typeerror,我是一个业余JS的人,所以请善待你的投票,因为我是新手 我得到了未捕获的类型错误: 无法读取null的属性“value” 我意识到问题在于我的js的顺序和它调用DOM的顺序,但我不确定具体在哪里 有人能帮我指出正确的方向吗 这是我的密码: <title></title> </head> <body> <form> <input id="todoItem" type="text" placeholder="Todo It
<title></title>
</head>
<body>
<form>
<input id="todoItem" type="text" placeholder="Todo Item" />
<input type="button" value="Save" onclick="insert();" />
</form>
<div id="display"></div>
<script type="text/javascript" src="main-2.js"></script>
</body>
</html>
main-2.js:
var todos = [];
var todoInput = document.getElementById("todoItem");
var messageBox = document.getElementById("display");
function insert() {
todos.push(todoInput.value);
clearAndShow();
}
function clearAndShow() {
todoInput.value = "";
messageBox.innerHTML = "";
messageBox.innerHTML += "Titles: " + todos.join(", ") + "<br/>";
}
var todos=[];
var todoInput=document.getElementById(“todoItem”);
var messageBox=document.getElementById(“显示”);
函数插入(){
todos.push(todoInput.value);
clearAndShow();
}
函数clearAndShow(){
todoInput.value=“”;
messageBox.innerHTML=“”;
messageBox.innerHTML+=“标题:”+todos.join(“,”)+“
”;
}
把这一行写上:
var todoInput = document.getElementById("todoItem");
在函数中插入insert
当var todoInput为init时,元素不存在
function insert() {
var todoInput = document.getElementById("todoItem");
todos.push(todoInput.value);
clearAndShow();
}
要清除输入,请编写此代码
document.getElementById("todoItem").value='';
这句话是:
var todoInput = document.getElementById("todoItem");
在函数中插入insert
当var todoInput为init时,元素不存在
function insert() {
var todoInput = document.getElementById("todoItem");
todos.push(todoInput.value);
clearAndShow();
}
要清除输入,请编写此代码
document.getElementById("todoItem").value='';
如果动态创建变量,则永远不会知道文档已加载。更好的方法是使用jquerydocument.ready,如果您在那里使用jquery
$(document).ready(function(){
//javascript code goes here
)};
或者通过这些修改尝试您的代码
<title></title>
</head>
<body>
<form>
<input id="todoItem" type="text" placeholder="Todo Item" />
<input type="button" value="Save" onclick="insert();" />
</form>
<div id="display"></div>
</body>
<script>
var todos = [];
function insert() {
var todoInput = document.getElementById("todoItem");
todos.push(todoInput.value);
clearAndShow();
}
function clearAndShow() {
var messageBox = document.getElementById("display");
todoInput.value = "";
messageBox.innerHTML = "";
messageBox.innerHTML += "Titles: " + todos.join(", ") + "<br/>";
}
</script>
var todos=[];
函数插入(){
var todoInput=document.getElementById(“todoItem”);
todos.push(todoInput.value);
clearAndShow();
}
函数clearAndShow(){
var messageBox=document.getElementById(“显示”);
todoInput.value=“”;
messageBox.innerHTML=“”;
messageBox.innerHTML+=“标题:”+todos.join(“,”)+“
”;
}
如果动态创建变量,您永远不会知道文档已加载。更好的方法是使用jquerydocument.ready,如果您在那里使用jquery
$(document).ready(function(){
//javascript code goes here
)};
或者通过这些修改尝试您的代码
<title></title>
</head>
<body>
<form>
<input id="todoItem" type="text" placeholder="Todo Item" />
<input type="button" value="Save" onclick="insert();" />
</form>
<div id="display"></div>
</body>
<script>
var todos = [];
function insert() {
var todoInput = document.getElementById("todoItem");
todos.push(todoInput.value);
clearAndShow();
}
function clearAndShow() {
var messageBox = document.getElementById("display");
todoInput.value = "";
messageBox.innerHTML = "";
messageBox.innerHTML += "Titles: " + todos.join(", ") + "<br/>";
}
</script>
var todos=[];
函数插入(){
var todoInput=document.getElementById(“todoItem”);
todos.push(todoInput.value);
clearAndShow();
}
函数clearAndShow(){
var messageBox=document.getElementById(“显示”);
todoInput.value=“”;
messageBox.innerHTML=“”;
messageBox.innerHTML+=“标题:”+todos.join(“,”)+“
”;
}
您所能投入的最佳时间是阅读和理解错误消息
Javascript中的典型或常见错误消息如下:
Uncaught TypeError: undefined is not a function
查看第一部分:uncaughttypeerror
Uncaught
表示未在catch
语句中捕获错误,而TypeError
是错误的名称
看看第二部分,它比第一部分更有用。从字面上看,它清楚地表明,它试图使用未定义的
作为函数
例如:
var x = undefined; // 'undefined' is a type in JS
x(); // This is what JS tried to do and said 'Hey! x is undefined. I can't execute it like a function.'
在您的情况下,错误的第二部分是:无法读取null的属性“value”。
从字面上想象这一点。属性上的某些上下文
var person = {
'name': 'Joe',
'age': 10
};
现在,person
是对象
,name
和age
是person
对象的属性
现在,如果执行person.name
JS,则可以执行它,因为person有一个名为name
的属性
但是,如果执行monster.name
,JS将尝试查找monster
对象。但是我们的代码中没有monster
对象。现在JS将抛出:
未捕获引用错误:未定义怪物
尝试分析这两部分:未捕获引用错误
。JS试图搜索或引用未找到的对象。因此它是ReferenceError
在您的例子中,当您尝试获取todoItem
时,DOM还没有准备好,因此JS无法找到它。所以它应该是一个引用错误
但是document.getElementById()
是搜索和修改标记中html
节点的ES实现,它是DOM
当找不到某个对象时将返回null
对象
null
是JS中的一个对象。就像person
是我们前面声明的对象一样
现在,变量todoInput
将从document.getElementById()接收null
对象
并尝试在null
对象中搜索value
属性。在你的错误的第二部分,它马上说:“嘿!我不能从null
对象中读取属性value
”你能做的最好的时间投资就是阅读和理解错误消息
Javascript中的典型或常见错误消息如下:
Uncaught TypeError: undefined is not a function
查看第一部分:uncaughttypeerror
Uncaught
表示未在catch
语句中捕获错误,而TypeError
是错误的名称
看看第二部分,它比第一部分更有用。从字面上看,它清楚地表明,它试图使用未定义的
作为函数
例如:
var x = undefined; // 'undefined' is a type in JS
x(); // This is what JS tried to do and said 'Hey! x is undefined. I can't execute it like a function.'
在您的情况下,错误的第二部分是:无法读取null的属性“value”。
从字面上想象这一点。属性上的某些上下文
var person = {
'name': 'Joe',
'age': 10
};
现在,person
是对象
,name
和age
是person
对象的属性
现在,如果执行person.name
JS,则可以执行它,因为person有一个名为name
的属性
但是,如果执行monster.name
,JS会尝试