javascript 无法读取未定义的属性“未定义”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14240006/
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 'undefined' of undefined
提问by lukeseager
Having a bit more trouble with Cookies, I did get my site to save cookies (to-do list items) and reload them after the page had been loaded and place them back into the table on screen.
使用 Cookie 时遇到了一些麻烦,我确实让我的网站保存了 cookie(待办事项列表项)并在页面加载后重新加载它们并将它们放回屏幕上的表格中。
But, when I deleted all the cookies and tried to start fresh I keep getting a console error saying Cannot read property 'undefined' of undefined
Now, if I comment out the below two sections of code, this problem goes away, but the cookie system will fail of course.
但是,当我删除所有 cookie 并尝试重新开始时,我不断收到控制台错误,说Cannot read property 'undefined' of undefined
现在,如果我注释掉以下两段代码,这个问题就会消失,但 cookie 系统当然会失败。
This section checks for the number of items that were in the to-do-list last time someone visited, and sets i
as that number, so nothing get's deleted.
此部分检查上次有人访问时待办事项列表中的项目数,并将其设置i
为该数字,因此不会删除任何内容。
var listCookies = document.cookie.split(";");
for(var a = 0; a < listCookies.length; a++) {
var listCount = myCookies[i].trim().split("=");
if(listCount[0].indexOf("listCount") === 0) {
var i = listCount;
} else {
var i = 0;
}
}
This section finds the actual to-do's themselves and places them in the table on screen.
此部分查找实际的待办事项并将它们放在屏幕上的表格中。
var myCookies = document.cookie.split(";");
for(var b = 0; b < myCookies.length; b++) {
var cookie = myCookies[i].trim().split("=");
if(cookie[0].indexOf("todo-") === 0) {
window.todoTable.insertAdjacentHTML('beforeend', decodeURIComponent(cookie[1]));
}
}
I understand the issue is that there aren't any cookies saved on someones first visit, but when I tried to add an if (typeof document.cookie === "undefined")
statement that didn't seem to do anything.
我了解问题在于某人第一次访问时没有保存任何 cookie,但是当我尝试添加if (typeof document.cookie === "undefined")
似乎没有任何作用的语句时。
I'm assuming I need an if statement wrapped around these pieces of code right? To say, if there isn't a cookie, 'skip this bit'. If there is, 'do stuff'.
我假设我需要一个 if 语句围绕这些代码片段,对吗?也就是说,如果没有 cookie,“跳过这一点”。如果有,“做事”。
回答by ATOzTOA
i
should be global, I have made it totalCount
.
i
应该是全球性的,我已经做到了totalCount
。
The first section should be:
第一部分应该是:
var totalCount = 0;
var listCookies = document.cookie.split(";");
for(var a = 0; a < listCookies.length; a++) {
var listCount = listCookies[a].trim().split("=");
if(listCount[0].indexOf("listCount") === 0) {
totalCount = listCount[1];
break;
}
}
Second section should be:
第二部分应该是:
var myCookies = document.cookie.split(";");
for(var b = 0; b < myCookies.length; b++) {
var cookie = myCookies[b].trim().split("=");
if(cookie[0].indexOf("todo-") === 0) {
window.todoTable.insertAdjacentHTML('beforeend', decodeURIComponent(cookie[1]));
}
}