无法读取未定义的属性“值”?(Cannot read property 'value' of undefined?)

编程入门 行业动态 更新时间:2024-10-28 10:25:51
无法读取未定义的属性“值”?(Cannot read property 'value' of undefined?)

要学习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>

更多推荐

本文发布于:2023-08-03 10:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1386875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   未定义   read   property   undefined

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!