根据变量创建localstorage计数器(无开关功能)

时间:2017-08-09 08:58:33

标签: javascript variables counter

我刚刚开始学习JavaScript,我正在尝试简化一些代码,但无法获得有效的解决方案。工作部分是:

switch (v) {

            case 0:
            if (localStorage.FGAz0) {
            localStorage.FGAz0 = Number(localStorage.FGAz0)+1;
            } else {
            localStorage.FGAz0 = 1;
            }
            document.getElementById("Ergebnis").innerHTML = "You have " + localStorage.FGAz0+ " Visitor(s).";
            break;  
            case 1:
            if (localStorage.FGAz1) {
            localStorage.FGAz1 = Number(localStorage.FGAz1)+1;
            } else {
            localStorage.FGAz1 = 1;
            }
            document.getElementById("Ergebnis").innerHTML = "You have " + localStorage.FGAz1+ " Visitor(s).";
            break;  
            case 2:
            if (localStorage.FGAz2) {
            localStorage.FGAz2 = Number(localStorage.FGAz2)+1;
            } else {
            localStorage.FGAz2 = 1;
            }
            document.getElementById("Ergebnis").innerHTML = "You have " + localStorage.FGAz2+ " Visitor(s).";   
            break;
            case 3:
            if (localStorage.FGAz3) {
            localStorage.FGAz3 = Number(localStorage.FGAz3)+1;
            } else {
            localStorage.FGAz3 = 1;
            }
            document.getElementById("Ergebnis").innerHTML = "You have " + localStorage.FGAz3+ " Visitor(s).";   
            break;
            case 4:
            if (localStorage.FGAz4) {
            localStorage.FGAz4 = Number(localStorage.FGAz4)+1;
            } else {
            localStorage.FGAz4 = 1;
            }
            document.getElementById("Ergebnis").innerHTML = "You have " + localStorage.FGAz4+ " Visitor(s).";
            break;
            case 5:
            if (localStorage.FGAz5) {
            localStorage.FGAz5 = Number(localStorage.FGAz5)+1;
            } else {
            localStorage.FGAz5 = 1;
            }
            document.getElementById("Ergebnis").innerHTML = "You have " + localStorage.FGAz5+ " Visitor(s).";
            break;
            default: 
            if (localStorage.FahrGastAnzahl) {
            localStorage.FahrGastAnzahl = Number(localStorage.FahrGastAnzahl)+1;
            } else {
            localStorage.FahrGastAnzahl= 1;
            }
            document.getElementById("Ergebnis").innerHTML = "You have " + localStorage.FahrGastAnzahl+ " Visitor(s).";}


    } else {
        document.getElementById("Ergebnis").innerHTML = "Sorry, dein Browser unterstützt die Speicherung von lokalen Daten nicht...";
    }

我试图将它缩短为var,取决于只有数字的“v”。目前我有这个:

if (localStorage.FGAz + "v") {
            (localStorage.FGAz + "v") = Number(localStorage.FGAz + "v")+1; 
        } else {
            (localStorage.FGAz + "v") = 1;
        document.getElementById("Ergebnis").innerHTML = "You have " + (localStorage.FGAz + "v") + " Visitor(s).";}

添加变量“v”有些不对劲,但我不知道什么,也没有找到解决方案。希望可以有人帮帮我。请不要jquery,我还没学到。

3 个答案:

答案 0 :(得分:1)

Firs of all ,确保您了解v"v"之间的区别:

  • v是一个变量名,可以包含任何值(字符串,数字等),例如:v = 1; v = "1"; v = "xxx";
  • "v"(请注意括号)是一个字符串值本身(v = "v",其中v是变量名称,"v"是变量值)。括号内的所有内容("')都是字符串。

如果您希望将数值(此示例中为整数)值转换为字符串,则可以使用v.toString()或只是将空字符串附加到值v + "" 。< / p>

其次,请了解Property Accessors

简而言之:您可以通过两种不同的方式访问对象的属性:使用点表示法(localStorage.FGAz1)或括号表示法(localStorage["FGAz1"])。

将字符串值附加到点标记属性访问器(document.getElementBy + "Id")将首先评估属性的值(document.getElementBy - 评估为undefined),然后THEN将它与字符串连接( undefined + "Id" - 会产生"undefinedId")。另一方面,将字符串附加到括号表示法(document["getElementBy" + "Id"])内的访问者值将首先评估访问者("getElementBy" + "Id" - 其评估为“getElementById”)的值,然后访问该对象的属性({ {1}} - 返回函数。)

根据您的示例:

document["getElementById"]未定义,因此localStorage.FGAz评估为localStorage.FGAz + "v",结果为undefined + "v"(请注意'v'添加到'undefined'的末尾)。句子"undefinedv"总是评估为真(HERE你可以找出原因)。

总结:

使用括号表示法: if ("undefinedv")。对于v = 1,这将评估为if (localStorage['FGAz' + v.toString()]) {,对于v = XXX,评估为if (localStorage['FGAz1']) {,依此类推。

我希望它对你有所帮助,如果不在评论中留下我的问题。

答案 1 :(得分:0)

而不是使用

localStorage.FGAz + "v"

使用

localStorage["FGAz" + v]

""引文使其成为字符串值不可变。

答案 2 :(得分:0)

我正在查看上面的代码,看起来你正在做某种访问者计数器。但是localstorage仅适用于一个人。

如果您打算使用vistor计数器,可以使用ajax将信息发送到数据库,然后检查有多少人在线。

简单访问计数器:

JSFIDDLE LINK

来自JSFIDDLE的代码

<div id="visitCount"></div>

if (localStorage.visitCount){
  localStorage.visitCount = Number(localStorage.visitCount)+1;
} else {
  localStorage.visitCount = 1;
}

document.getElementById("visitCount").innerHTML = "You've been to this page "+ localStorage.visitCount + " time(s)."