JavaScript
var textarea = document.getElementById("textarea").value;
HTML
<textarea id="textarea" cols="100" rows="10">du hello!</textarea>
最佳答案
TypeError: Result of expression
'document.getElementById("textarea")' [null]
is not an object.
此错误表明它找不到 id 为“textarea”的元素。因此,此 getElementById
操作的结果为 null
,并且您无法访问 null
上的 .value
,因为它没有任何属性。
根本原因是搜索时不存在id为“textarea”的元素。要么您在某个地方出现拼写错误,要么您的脚本在该元素尚不存在时运行。如果您的脚本位于标题中,您需要这样编写:
document.ready = function () {
... script here ...
}
这将延迟执行,直到文档准备好并且所有元素都存在。
关于javascript - 将文本区域的值应用于变量时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2597448/