javascript 无法读取未定义的属性“值”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5999042/
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
Cannot read property 'value' of undefined?
提问by Dr Hydralisk
To learn JavaScript I am writing a small text editor Google Chrome extension. But I keep getting this error: Cannot read property 'value' of undefined
. It happens whenever tranquility.save();
is called, but not when tranquility.open();
which is weird because they are basically the same, just switched the sides. paper
is just a <textarea>
.
为了学习 JavaScript,我正在编写一个小型文本编辑器 Google Chrome 扩展程序。但我不断收到此错误:Cannot read property 'value' of undefined
。每当tranquility.save();
被调用时都会发生,但不会发生tranquility.open();
,这很奇怪,因为它们基本相同,只是换了一边。paper
只是一个<textarea>
.
var tranquility = {
paper: document.getElementById("paper"),
lastOpenedPaper: localStorage.getItem("lastOpenedPaper"),
listen: function() {
this.paper.addEventListener("keyup", this.save, false);
},
save: function() {
localStorage.setItem(this.lastOpenedPaper, this.paper.value);
},
open: function() {
this.paper.value = localStorage.getItem(this.lastOpenedPaper);
}
}
EDIT:
编辑:
It is called after <textarea>
is created (unless it has to be the entire DOM)
它在<textarea>
创建后调用(除非它必须是整个 DOM)
<body>
<textarea id="paper"></textarea>
<script src="../js/application.js"></script>
<script>
tranquility.listen();
</script>
</body>
采纳答案by KoolKabin
I got it working with two fixes
我得到了两个修复
1.) Write the script after loading of paper textarea. It was done by placing script at the end of body.
1.) 在加载纸张 textarea 后编写脚本。它是通过将脚本放在正文的末尾来完成的。
2.) I found localStorage.setItem
and localStorage.getItem
should refer to the variable name like in http://hacks.mozilla.org/2009/06/localstorage/. In your code lastOpenedPaper would refer to null value in begining so it won't store value in that name so i tried replacing it with just. you can use other var name too or use varname directly as localStorage.setItem('anyvarname', this.paper.value);
2.) 我发现localStorage.setItem
并localStorage.getItem
应该引用变量名,如http://hacks.mozilla.org/2009/06/localstorage/。在您的代码中,lastOpenedPaper 会在开始时引用空值,因此它不会在该名称中存储值,因此我尝试将其替换为 just。您也可以使用其他 var 名称或直接使用 varname 作为localStorage.setItem('anyvarname', this.paper.value);
code:
代码:
<script>
var tranquility = {
paper: document.getElementById("paper"),
lastOpenedPaper: 'just',
listen: function() {
this.paper.addEventListener("keyup", this.save, false);
},
save: function() {
localStorage.setItem(this.lastOpenedPaper, this.value);
},
open: function() {
this.paper.value = localStorage.getItem(this.lastOpenedPaper);
}
}
</script>
回答by onteria_
It happens whenever tranquility.save(); is called, but not when tranquility.open(); which is weird because they are basically the same, just switched the sides.
它发生在宁静.save(); 被调用,但不是在 quiet.open(); 这很奇怪,因为它们基本相同,只是换了一边。
Ah but they're not the same because:
啊,但它们不一样,因为:
save: function() {
localStorage.setItem(this.lastOpenedPaper, this.paper.value);
},
This is potentially not okay, because this.paper.value
could not be defined
这可能不太好,因为this.paper.value
无法定义
open: function() {
this.paper.value = localStorage.getItem(this.lastOpenedPaper);
}
Where this is setting a value to this.paper.value
, so it being defined or not doesn't matter.
这是将值设置为 的位置this.paper.value
,因此是否定义它并不重要。
Also:
还:
document.getElementById("paper")
This won't work if this code is not declared onload
or after the <textarea>
is created, because the DOM tree hasn't been built otherwise.
如果此代码未声明onload
或在<textarea>
创建之后,这将不起作用,因为 DOM 树尚未构建。