javascript 类型错误为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20720742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:06:45  来源:igfitidea点击:

TypeError is Null

javascripthtmluser-inputtypeerror

提问by RD5K

I'm fixing a simple user input mulptiplication table which has been littered with errors. I'm stuck with a partucular piece of code and do not understand what it means.

我正在修复一个简单的用户输入乘法表,该表充斥着错误。我被一段特定的代码困住了,不明白它的意思。

When the script is run in firbug it says "TypeError: document.getElementById(...) is null"

当脚本在 firbug 中运行时,它说 "TypeError: document.getElementById(...) is null"

This is the code which is attached to html:

这是附加到html的代码:

var get = function(name){return document.getElementById("name").value;};

var set = function(name,value){document.getElementById("name").value=value;};

回答by plalx

That simply means that there is no element with an idproperty of "name"in the DOM. Perhaps your code runs before the document is ready?

这只是意味着在 DOM 中没有具有id属性的元素"name"。也许您的代码在文档准备好之前运行?

回答by pdoherty926

It looks like this code is meant to query the DOM for an element with the id of name:

看起来这段代码是为了在 DOM 中查询 id 为 的元素name

var get = function(name){return document.getElementById(name).value;};
var set = function(name,value){document.getElementById(name).value=value;};

回答by paraselena

It means that the element with id = "name"is not found in document. Look in the dom if it exists. You can also try to add this code as event handler for ready-event to check that it works ok there - as it is already said maybe you run this code before the dom is loaded.

这意味着id = "name"在文档中找不到元素 with 。查看dom是否存在。您还可以尝试将此代码添加为ready-event 的事件处理程序,以检查它在那里是否正常工作 - 正如已经说过的,也许您在加载 dom 之前运行了此代码。