2

我有以下代码:

var windowNow = window.localStorage.getItem("windowNow");
switch(windowNow)
{
case 1:
    var link = "http://www.zive.sk/rss/sc-47/default.aspx";
    var listviewID = "feedZive";
    break;
case 2:
    var link = "http://mobilmania.azet.sk/rss/sc-47/default.aspx";
    var listviewID = "feedMobil";
    break;
case 3: 
    var link = "http://www.automoto.sk/rss";
    var listviewID = "feedAuto";
    break;
}

我知道,windowNow === 1因为我已经用 alert 进行了检查,并且为了确保它真的是 1 我检查了它if(windowNow == 1) { alert ("Window now is 1");}并且它有效。但它在我的开关内不起作用(通过警报检查)。

4

2 回答 2

8

中的项目localStorage始终是字符串。使用case "1"等。

您的检查的问题是这是一个松散的检查,它不检查数据类型。你应该尝试过

if(windowNow === 1) { alert ("Window now is 1");}

注意三元组=

于 2012-09-12T15:11:05.553 回答
2

不要在开关内声明变量。在交换机外部声明它们并在内部分配。还要先转换为整数。

var windowNow = parseInt(window.localStorage.getItem("windowNow"), 10), link, listviewID;
switch(windowNow)
{
case 1:
    link = "http://www.zive.sk/rss/sc-47/default.aspx";
    listviewID = "feedZive";
    break;
case 2:
    link = "http://mobilmania.azet.sk/rss/sc-47/default.aspx";
    listviewID = "feedMobil";
    break;
case 3: 
    link = "http://www.automoto.sk/rss";
    listviewID = "feedAuto";
    break;
default:
    // default assignment.
}
于 2012-09-12T15:11:24.613 回答