将localStorage字符串设置为Integer

时间:2016-05-10 11:26:30

标签: javascript html html5 local-storage

我正在尝试为我的游戏制作一个保存功能,它不允许我将任何变量保存为整数(即​​使使用parseInt(变量),+变量等)。

this帖子的答案无效

完整代码:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Code Clicker</title>
<style>
html {
    padding-left: 20px;
    padding-right: 20px;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -webkit-touch-callout: none;
}
#nav, .navtxt {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    margin-left: 20px;
}
#nav li, .navtxt {
    display: block;
    color: #000000;
    text-align: center;
    padding: 5px 15px;
    text-decoration: none;
    float: left;
    font-size: 10;
    border-right: 1.5px solid;
    border-left: 1.5px solid;
    border-top: 1.5px solid;
}
.active {
    background-color: #666666;
    font-weight: bold;
}
#nav li:not(.active) {
    background-color: #A9A9A9;
}
.navtxt {
    background-color: #FFFFFF;
    float: right;
}
.hidden {
    display: none;
}
div:not(.open) {
    display: none;  
}
div {
    padding-left: 25px;
    padding-right: 25px;
}
#learn {
    margin-left: 20px;
    margin-right: 20px;
}
.main {
    background: #F0F0F0;
    border: 5px solid;
}
.sub {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: -2px;
}
</style>
</head>
<body style="background: lightblue;">
<p style="text-align: center; font-size: 35px;"> Code Clicker </p>
<ul id="nav">
<li id="navLvlup" class="active" onClick="pageLvlup()"><a>LEVEL UP</a></li>
<li id="navJobs" onClick="pageJobs()">JOBS</li>
<li id="navStats" onClick="pageStats()">STATS</li>
<li id="navUpgrade" onClick="pageUpgrade()">UPGRADE</li>
<span class="navtxt">Money: $<span id="money"></span></span>
<span class="navtxt" onCLick="load()">LOAD</span>
<span class="navtxt" onClick="save()">SAVE</span>
</ul>
<div class="open main">
<div id="lvlup" class="open">
<p>Learn HTML <button class="learn" onClick="learnHTML()">Learn</button> <span id="HTMLxp"></span>/<span id="HTMLxpMax"></span></p>
<p>Learn CSS <button class="learn" onClick="learnCSS()">Learn</button> <span id="CSSxp"></span>/<span id="CSSxpMax"></span></p>
<p>Learn JS <button class="learn" onClick="learnJS()">Learn</button> <span id="JSxp"></span>/<span id="JSxpMax"></span></p>
<p>Learn PHP <button class="learn" onClick="learnPHP()">Learn</button> <span id="PHPxp"></span>/<span id="PHPxpMax"></span></p>
<p>Learn Python <button class="learn" onClick="learnPY()">Learn</button> <span id="PYxp"></span>/<span id="PYxpMax"></span></p>
</div>
<div id="jobs">
<p onClick="basicWebDisplay()">Basic Website <span id="basicWebCompT"> </span>/<span id="basicWebCompMaxT"> </span></p>
<div id="basicWeb" class="">
<button class="learn" onClick="jobBasicWeb()">Code</button>
<p>Get Paid: <span id="basicWebPay"></span></p>
<p>Completion: <span id="basicWebComp"></span>/<span id="basicWebCompMax"></span></p>
<p><span>Assign HTML Coders </span><button onClick="addBasicWebHTMLCoders()">+</button><button onClick="takeBasicWebHTMLCoders()">-</button><span id="basicWebHTMLCoders"></span></p>
<p><span>Assign CSS Coders </span><button onClick="addBasicWebCSSCoders()">+</button><button onClick="takeBasicWebCSSCoders()">-</button><span id="basicWebCSSCoders"></span></p>
</div>
<p onClick="interactiveWebDisplay()">Interactive Website <span id="interactiveWebCompT"> </span>/<span id="interactiveWebCompMaxT"> </span></p>
<div id="interactiveWeb" class="">
<button class="learn" onClick="jobInteractiveWeb()">Code</button>
<p>Get Paid: <span id="interactiveWebPay"></span></p>
<p>Completion: <span id="interactiveWebComp"></span>/<span id="interactiveWebCompMax"></span></p>
<p><span>Assign HTML Coders </span><button onClick="addInteractiveWebHTMLCoders()">+</button><button onClick="takeInteractiveWebHTMLCoders()">-</button><span id="interactiveWebHTMLCoders"></span></p>
<p><span>Assign CSS Coders </span><button onClick="addInteractiveWebCSSCoders()">+</button><button onClick="takeInteractiveWebCSSCoders()">-</button><span id="interactiveWebCSSCoders"></span></p>
<p><span>Assign JS Coders </span><button onClick="addInteractiveWebJSCoders()">+</button><button onClick="takeInteractiveWebJSCoders()">-</button><span id="interactiveWebJSCoders"></span></p>
</div>
</div>
<div id="stats">
<p class="sub">LEVELS:</p>
<p>HTML: <span id="HTMLlvl"> 0 </span></p>
<p>CSS: <span id="CSSlvl"> 0 </span></p>
<p>JS: <span id="JSlvl"> 0 </span></p>
<p>PHP: <span id="PHPlvl"> 0 </span></p>
<p>Python: <span id="PYlvl"> 0 </span></p>
</div>
<div id="upgrade">
<p>Upgrade Learn Speed <button id="uplearn" class="learn" onClick="upLearn()">$100</button></p>
<p>Hire a HTML Coder <button class="learn" onClick="HTMLcoder()">$150</button></p>
<p>Hire a CSS Coder <button class="learn" onClick="CSScoder()">$200</button></p>
</div>
</div>
<script>
// General Variables
var money = 0;
var openPage = 1;
var learnSpeed = 1;
var basicWebDisplayN = 0;
var interactiveWebDisplayN = 0;
// HTML Variables
var HTMLxp = 0;
var HTMLlvl = 0;
var HTMLxpSMax = 50;
var HTMLxpMax = HTMLxpSMax;
// CSS Variables
var CSSxp = 0;
var CSSlvl = 0;
var CSSxpSMax = 50;
var CSSxpMax = CSSxpSMax;
// JS Variables
var JSxp = 0;
var JSlvl = 0;
var JSxpSMax = 50;
var JSxpMax = JSxpSMax;
// PHP Variables
var PHPxp = 0;
var PHPlvl = 0;
var PHPxpSMax = 50;
var PHPxpMax = PHPxpSMax;
// Python Variables
var PYxp = 0;
var PYlvl = 0;
var PYxpSMax = 50;
var PYxpMax = PYxpSMax;
// HTML Coders
var HTMLcoders = 0;
var basicWebHTMLCoders = 0;
var interactiveWebHTMLCoders = 0;
// CSS Coders
var CSScoders = 0;
var basicWebCSSCoders = 0;
var interactiveWebCSSCoders = 0;
// JS Coders
var JScoders = 0;
var interactiveWebJSCoders = 0;
// Basic Website (Job)
var basicWebPayS = 100;
var basicWebPay = basicWebPayS;
var basicWebComp = 0;
var basicWebCompMaxS = 150;
var basicWebCompMax = basicWebCompMaxS;
var basicWebComplete = 0;
var basicWebCompShow = 1;
// Basic Website Coders
var basicWebCoders = 0;
// Interactive Website (Job)
var interactiveWebPayS = 100;
var interactiveWebPay = interactiveWebPayS;
var interactiveWebComp = 0;
var interactiveWebCompMaxS = 150;
var interactiveWebCompMax = interactiveWebCompMaxS;
var interactiveWebComplete = 0;
var interactiveWebCompShow = 1;
// Interactive Website Coders
var interactiveWebCoders = 0;

function save()
{
    localStorage.money = money;
    localStorage.openPage = openPage;
    localStorage.learnSpeed = learnSpeed;
    localStorage.basicWebDisplayN = basicWebDisplayN;
    localStorage.interactiveWebDisplayN = interactiveWebDisplayN;
    // HTML Variables
    localStorage.HTMLxp = HTMLxp;
    localStorage.HTMLlvl = HTMLlvl;
    localStorage.HTMLxpSMax = HTMLxpSMax;
    localStorage.HTMLxpMax = HTMLxpMax;
    // CSS Variables
    localStorage.CSSxp = CSSxp;
    localStorage.CSSlvl = CSSlvl;
    localStorage.CSSxpSMax = CSSxpSMax;
    localStorage.CSSxpMax = CSSxpMax;
    // JS Variables
    localStorage.JSxp = JSxp;
    localStorage.JSlvl = JSlvl;
    localStorage.JSxpSMax = JSxpSMax;
    localStorage.JSxpMax = JSxpMax;
    // PHP Variables
    localStorage.PHPxp = PHPxp;
    localStorage.PHPlvl = PHPlvl;
    localStorage.PHPxpSMax = PHPxpSMax;
    localStorage.PHPxpMax = PHPxpMax;
    // Python Variables
    localStorage.PYxp = PYxp;
    localStorage.PYlvl = PYlvl;
    localStorage.PYxpSMax = PYxpSMax;
    localStorage.PYxpMax = PYxpMax;
    // HTML Coders
    localStorage.HTMLcoders = HTMLcoders;
    localStorage.basicWebHTMLCoders = basicWebHTMLCoders;
    localStorage.interactiveWebHTMLCoders = interactiveWebHTMLCoders;
    // CSS Coders
    localStorage.CSScoders = CSScoders;
    localStorage.basicWebCSSCoders = basicWebCSSCoders;
    localStorage.interactiveWebCSSCoders = interactiveWebCSSCoders;
    // JS Coders
    localStorage.JScoders = JScoders;
    localStorage.interactiveWebJSCoders = interactiveWebJSCoders;
    // Basic Website (Job)
    localStorage.basicWebPayS = basicWebPayS;
    localStorage.basicWebPay = basicWebPay;
    localStorage.basicWebComp = basicWebComp;
    localStorage.basicWebCompMaxS = basicWebCompMaxS;
    localStorage.basicWebCompMax = basicWebCompMax;
    localStorage.basicWebComplete = basicWebComplete;
    localStorage.basicWebCompShow = basicWebCompShow;
    // Basic Website Coders
    localStorage.basicWebCoders = basicWebCoders;
    // Interactive Website (Job)
    localStorage.interactiveWebPayS = interactiveWebPayS;
    localStorage.interactiveWebPay = interactiveWebPay;
    localStorage.interactiveWebComp = interactiveWebComp;
    localStorage.interactiveWebCompMaxS = interactiveWebCompMaxS;
    localStorage.interactiveWebCompMax = interactiveWebCompMax;
    localStorage.interactiveWebComplete = interactiveWebComplete;
    localStorage.interactiveWebCompShow = interactiveWebCompShow;
    // Interactive Website Coders
    localStorage.interactiveWebCoders = interactiveWebCoders;
}
function load()
{
    money = localStorage.money;
    openPage = localStorage.openPage;
    learnSpeed = localStorage.learnSpeed;
    basicWebDisplayN = localStorage.basicWebDisplayN;
    interactiveWebDisplayN = localStorage.interactiveWebDisplayN;
    // HTML Variables
    HTMLxp = localStorage.HTMLxp;
    HTMLlvl = localStorage.HTMLlvl;
    HTMLxpSMax = localStorage.HTMLxpSMax;
    HTMLxpMax = localStorage.HTMLxpMax;
    // CSS Variables
    CSSxp = localStorage.CSSxp;
    CSSlvl = localStorage.CSSlvl;
    CSSxpSMax = localStorage.CSSxpSMax;
    CSSxpMax = localStorage.CSSxpMax;
    // JS Variables
    JSxp = localStorage.JSxp;
    JSlvl = localStorage.JSlvl;
    JSxpSMax = localStorage.JSxpSMax;
    JSxpMax = localStorage.JSxpMax;
    // PHP Variables
    PHPxp = localStorage.PHPxp;
    PHPlvl = localStorage.PHPlvl;
    PHPxpSMax = localStorage.PHPxpSMax;
    PHPxpMax = PHPxpMax;
    // Python Variables
    PYxp = localStorage.PYxp;
    PYlvl = localStorage.PYlvl;
    PYxpSMax = localStorage.PYxpSMax;
    PYxpMax = localStorage.PYxpMax;
    // HTML Coders
    HTMLcoders = localStorage.HTMLcoders;
    basicWebHTMLCoders = localStorage.basicWebHTMLCoders;
    interactiveWebHTMLCoders = localStorage.interactiveWebHTMLCoders;
    // CSS Coders
    CSScoders = localStorage.CSScoders;
    basicWebCSSCoders = localStorage.basicWebCSSCoders;
    interactiveWebCSSCoders = localStorage.interactiveWebCSSCoders;
    // JS Coders
    JScoders = localStorage.JScoders;
    interactiveWebJSCoders = localStorage.interactiveWebJSCoders;
    // Basic Website (Job)
    basicWebPayS = localStorage.basicWebPayS;
    basicWebPay = localStorage.basicWebPay;
    basicWebComp = localStorage.basicWebComp;
    basicWebCompMaxS = localStorage.basicWebCompMaxS;
    basicWebCompMax = localStorage.basicWebCompMax;
    basicWebComplete = localStorage.basicWebComplete;
    basicWebCompShow = localStorage.basicWebCompShow;
    // Basic Website Coders
    basicWebCoders = localStorage.basicWebCoders;
    // Interactive Website (Job)
    interactiveWebPayS = localStorage.interactiveWebPayS;
    interactiveWebPay = localStorage.interactiveWebPay;
    interactiveWebComp = localStorage.interactiveWebComp;
    interactiveWebCompMaxS = localStorage.interactiveWebCompMaxS;
    interactiveWebCompMax = localStorage.interactiveWebCompMax;
    interactiveWebComplete = localStorage.nteractiveWebComplete;
    interactiveWebCompShow = localStorage.interactiveWebCompShow;
    // Interactive Website Coders
    interactiveWebCoders = localStorage.interactiveWebCoders;
}
function pageLvlup()
{
    openPage = 1;
    checkPage();
}
function pageJobs()
{
    openPage = 2;
    checkPage();
}
function pageStats()
{
    openPage = 3;
    checkPage();
}
function pageUpgrade()
{
    openPage = 4;
    checkPage();
}
function checkPage()
{
    if (openPage ==     1)
    {
        document.getElementById("lvlup").className = "open";
        document.getElementById("jobs").className = "";
        document.getElementById("stats").className = "";
        document.getElementById("upgrade").className = "";
        document.getElementById("navLvlup").className = "active";
        document.getElementById("navJobs").className = "";
        document.getElementById("navStats").className = "";
        document.getElementById("navUpgrade").className = "";
    }
    if (openPage ==     2)
    {
        document.getElementById("lvlup").className = "";
        document.getElementById("jobs").className = "open";
        document.getElementById("stats").className = "";
        document.getElementById("upgrade").className = "";  
        document.getElementById("navLvlup").className = "";
        document.getElementById("navJobs").className = "active";
        document.getElementById("navStats").className = "";
        document.getElementById("navUpgrade").className = "";
    }
    if (openPage ==     3)
    {
        document.getElementById("lvlup").className = "";
        document.getElementById("jobs").className = "";
        document.getElementById("stats").className = "open";
        document.getElementById("upgrade").className = "";      
        document.getElementById("navLvlup").className = "";
        document.getElementById("navJobs").className = "";
        document.getElementById("navStats").className = "active";
        document.getElementById("navUpgrade").className = "";
    }
    if (openPage ==     4)
    {
        document.getElementById("lvlup").className = "";
        document.getElementById("jobs").className = "";
        document.getElementById("stats").className = "";
        document.getElementById("upgrade").className = "open";  
        document.getElementById("navLvlup").className = "";
        document.getElementById("navJobs").className = "";
        document.getElementById("navStats").className = "";
        document.getElementById("navUpgrade").className = "active";
    }
}
function upLearn()
{
    if (money >= 100)
    {
        money = money - 100;
        learnSpeed = learnSpeed + 1;
        document.getElementById("uplearn").innerHTML = "PURCHASED";
    }
}
function learnHTML()
{
    HTMLxp = HTMLxp + learnSpeed;
    if (HTMLxp >= HTMLxpMax)
    {
        HTMLxp = 0;
        HTMLlvl = HTMLlvl + 1;
        HTMLxpMax = HTMLxpSMax * (HTMLlvl + 1);
    }
}
function learnCSS()
{
    CSSxp = CSSxp + learnSpeed;
    if (CSSxp >= CSSxpMax)
    {
        CSSxp = 0;
        CSSlvl = CSSlvl + 1;
        CSSxpMax = CSSxpSMax * (CSSlvl + 1);
    }
}
function learnJS()
{
    JSxp = JSxp + learnSpeed;
    if (JSxp >= JSxpMax)
    {
        JSxp = 0;
        JSlvl = JSlvl + 1;
        JSxpMax = JSxpSMax * (JSlvl + 1);
    }
}
function learnPY()
{
    PHPxp = PHPxp + learnSpeed;
    if (PHPxp >= PHPxpMax)
    {
        PYxp = 0;
        PYlvl = PYlvl + 1;
        PYxpMax = PYxpSMax * (PYlvl + 1);
    }
}
function learnPHP()
{
    PHPxp = PHPxp + learnSpeed;
    if (PHPxp >= PHPxpMax)
    {
        PHPxp = 0;
        PHPlvl = PHPlvl + 1;
        PHPxpMax = PHPxpSMax * (PHPlvl + 1);
    }
}
function jobBasicWeb()
{
    if (HTMLlvl >= 1 && CSSlvl>= 1)
    {
        basicWebComp = basicWebComp + (HTMLlvl + CSSlvl);
        checkBasicWebComp();
    }
}
function HTMLcoder()
{
    if (money >= 150)
    {
        HTMLcoders = HTMLcoders + 1;
        money = money - 150;
    }
}
function CSScoder()
{
    if (money >= 200)
    {
        CSScoders = CSScoders + 1;
        money = money - 200;
    }
}
function addBasicWebHTMLCoders()
{
    if (HTMLcoders >= 1)
    {
        HTMLcoders = HTMLcoders - 1;
        basicWebHTMLCoders = basicWebHTMLCoders + 1;
    }
}
function takeBasicWebHTMLCoders()
{
    if (basicWebHTMLCoders >= 1)
    {
        HTMLcoders = HTMLcoders + 1;
        basicWebHTMLCoders = basicWebHTMLCoders - 1;
    }
}
function addBasicWebCSSCoders()
{
    if (CSScoders >= 1)
    {
        CSScoders = CSScoders - 1;
        basicWebCSSCoders = basicWebCSSCoders + 1;
    }
}
function takeBasicWebCSSCoders()
{
    if (basicWebCSSCoders >= 1)
    {
        CSScoders = CSScoders + 1;
        basicWebCSSCoders = basicWebCSSCoders - 1;
    }
}
function basicWebCodersWork()
{
    basicWebComp = basicWebComp + basicWebCoders;
    checkBasicWebComp();
}
function checkBasicWebComp()
{
    if (basicWebComp >= basicWebCompMax)
    {
        basicWebComp = 0;
        basicWebComplete = basicWebComplete + 1;
        money = money + basicWebPay;
        basicWebCompMax = basicWebComplete * basicWebCompMaxS;
        basicWebPay = basicWebComplete * basicWebPayS;
    }
}
function basicWebDisplay()
{
    if (basicWebDisplayN === 0)
    {
        document.getElementById("basicWeb").className = "open";
        basicWebCompShow = 0;
        basicWebDisplayN = 1;
    }
    else if (basicWebDisplayN == 1)
    {
        document.getElementById("basicWeb").className = "";
        basicWebDisplayN = 0;
        basicWebCompShow = 1;
    }
}
function jobInteractiveWeb()
{
    if (HTMLlvl >= 1 && CSSlvl>= 1)
    {
        interactiveWebComp = interactiveWebComp + (HTMLlvl + CSSlvl);
        checkInteractiveWebComp();
    }
}
function addInteractiveWebHTMLCoders()
{
    if (HTMLcoders >= 1)
    {
        HTMLcoders = HTMLcoders - 1;
        interactiveWebHTMLCoders = interactiveWebHTMLCoders + 1;
    }
}
function takeInteractiveWebHTMLCoders()
{
    if (interactiveWebHTMLCoders >= 1)
    {
        HTMLcoders = HTMLcoders + 1;
        interactiveWebHTMLCoders = interactiveWebHTMLCoders - 1;
    }
}
function addInteractiveWebCSSCoders()
{
    if (CSScoders >= 1)
    {
        CSScoders = CSScoders - 1;
        interactiveWebCSSCoders = interactiveWebCSSCoders + 1;
    }
}
function takeInteractiveWebCSSCoders()
{
    if (interactiveWebCSSCoders >= 1)
    {
        CSScoders = CSScoders + 1;
        interactiveWebCSSCoders = interactiveWebCSSCoders - 1;
    }
}
function interactiveWebCodersWork()
{
    interactiveWebComp = interactiveWebComp + interactiveWebCoders;
    checkInteractiveWebComp();
}
function checkInteractiveWebComp()
{
    if (interactiveWebComp >= interactiveWebCompMax)
    {
        interactiveWebComp = 0;
        interactiveWebComplete = interactiveWebComplete + 1;
        money = money + interactiveWebPay;
        interactiveWebCompMax = interactiveWebComplete * interactiveWebCompMaxS;
        interactiveWebPay = interactiveWebComplete * interactiveWebPayS;
    }
}
function interactiveWebDisplay()
{
    if (interactiveWebDisplayN === 0)
    {
        document.getElementById("interactiveWeb").className = "open";
        interactiveWebCompShow = 0;
        interactiveWebDisplayN = 1;
    }
    else if (interactiveWebDisplayN == 1)
    {
        document.getElementById("interactiveWeb").className = "";
        interactiveWebDisplayN = 0;
        interactiveWebCompShow = 1;
    }
}
function updateAll()
{
    basicWebCoders = basicWebHTMLCoders + basicWebCSSCoders;
    interactiveWebCoders = interactiveWebHTMLCoders + interactiveWebCSSCoders;
    document.getElementById("money").innerHTML = money;
    document.getElementById("HTMLlvl").innerHTML = HTMLlvl;
    document.getElementById("HTMLxp").innerHTML = HTMLxp;
    document.getElementById("HTMLxpMax").innerHTML = HTMLxpMax;
    document.getElementById("CSSlvl").innerHTML = CSSlvl;
    document.getElementById("CSSxp").innerHTML = CSSxp;
    document.getElementById("CSSxpMax").innerHTML = CSSxpMax;
    document.getElementById("JSlvl").innerHTML = JSlvl;
    document.getElementById("JSxp").innerHTML = JSxp;
    document.getElementById("JSxpMax").innerHTML = JSxpMax;
    document.getElementById("PYlvl").innerHTML = PYlvl;
    document.getElementById("PYxp").innerHTML = PYxp;
    document.getElementById("PYxpMax").innerHTML = PYxpMax;
    document.getElementById("PHPlvl").innerHTML = PHPlvl;
    document.getElementById("PHPxp").innerHTML = PHPxp;
    document.getElementById("PHPxpMax").innerHTML = PHPxpMax;
    document.getElementById("basicWebPay").innerHTML = basicWebPay;
    document.getElementById("basicWebComp").innerHTML = basicWebComp;
    document.getElementById("basicWebCompMax").innerHTML = basicWebCompMax;
    document.getElementById("basicWebHTMLCoders").innerHTML = basicWebHTMLCoders;
    document.getElementById("basicWebCSSCoders").innerHTML = basicWebCSSCoders;
    document.getElementById("interactiveWebPay").innerHTML = interactiveWebPay;
    document.getElementById("interactiveWebComp").innerHTML = interactiveWebComp;
    document.getElementById("interactiveWebCompMax").innerHTML = interactiveWebCompMax;
    document.getElementById("interactiveWebHTMLCoders").innerHTML = interactiveWebHTMLCoders;
    document.getElementById("interactiveWebCSSCoders").innerHTML = interactiveWebCSSCoders;
    document.getElementById("interactiveWebJSCoders").innerHTML = interactiveWebJSCoders;
    document.getElementById("basicWebCompT").innerHTML = basicWebComp;
    document.getElementById("basicWebCompMaxT").innerHTML = basicWebCompMax;
    document.getElementById("interactiveWebCompT").innerHTML = interactiveWebComp;
    document.getElementById("interactiveWebCompMaxT").innerHTML = interactiveWebCompMax;
}
setInterval(updateAll, 100);
setInterval(basicWebCodersWork, 2000);
setInterval(interactiveWebCodersWork, 2000);
</script>
</body>
</html>

请注意saveload功能

再次注意我已尝试HTMLxp = parseInt(localStorage.HTMLxp;)HTMLxp = localStorage.HTMLxp * 1;等等。

如果需要,您可以查看脚本/页面here

3 个答案:

答案 0 :(得分:1)

尝试使用var x = parseInt(localstorage.numeric)

&#13;
&#13;
function populateStorage() {
  var obj = {x:"sdfsd", y:"sdfsdf"};
  
  localStorage.setItem('image', JSON.stringify(obj));
  console.log(localStorage.getItem('image'));
  
  localStorage.bgcolor = 'blue'
  console.log(localStorage.bgcolor);
  
  //working with numeric values
  localStorage.numeric = 3;
  console.log( parseInt(localStorage.numeric) + 1);
}

//just calling calling the funtion
var x = populateStorage.call();
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以将一个对象存储在本地存储中,如下所示

  Storage.prototype.setObject = function(key, value) {
    this.setItem(key, JSON.stringify(value));
}

Storage.prototype.getObject = function(key) {
    var value = this.getItem(key);
    return value && JSON.parse(value);
};


    localStorage.setObject('x', {1: 2, 2: "s"})
    console.log(localStorage.getObject('x'));

答案 2 :(得分:0)

localStorage中任意数字数据从字符串转换为数字的最佳方式:

var a = localStorage['some_property'];

typeof a; // "string"

var b = +localStorage['some_property'];

typeof b; // "number"