JavaScript:需要帮助订购我的JS吗;未捕获类型错误:无法读取属性';价值';“无效”的定义;

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

我是一个业余JS的人,所以请善待你的投票,因为我是新手

我得到了未捕获的类型错误:

无法读取null的属性“value”

我意识到问题在于我的js的顺序和它调用DOM的顺序,但我不确定具体在哪里

有人能帮我指出正确的方向吗

这是我的密码:

<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会尝试