要学习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); } }编辑:
它是在创建<textarea>之后调用的(除非它必须是整个DOM)
<body> <textarea id="paper"></textarea> <script src="../js/application.js"></script> <script> tranquility.listen(); </script> </body>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>.
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)
<body> <textarea id="paper"></textarea> <script src="../js/application.js"></script> <script> tranquility.listen(); </script> </body>最满意答案
我得到了两个修复程序
1.)在加载纸张文本区域后编写脚本。 这是通过将脚本放在正文的末尾来完成的。
2.)我发现localStorage.setItem和localStorage.getItem应该像http://hacks.mozilla.org/2009/06/localstorage/中那样引用变量名。 在你的代码中,lastOpenedPaper会在开始时引用null值,因此它不会在该名称中存储值,所以我尝试用just替换它。 您也可以使用其他var名称或直接使用varname作为localStorage.setItem('anyvarname', this.paper.value);
码:
<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>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.
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);
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>更多推荐
发布评论