【问题标题】:Javascript sessionstorage: unexpected value changeJavascript会话存储:意外的值更改
【发布时间】:2015-09-24 01:29:36
【问题描述】:

我为我的网站制作了一个小的“字体大小小部件”。现在,今天我正在寻求实现会话存储,这样用户就不必一直点击按钮。

标准字体大小是 20。现在,我不知道为什么,但有时,只有当我点击 button2(为了获得更大的大小)时,值才会变成字面上的 20 + 2 => 202。我不知道如何或为什么。

有人知道如何提高性能并解决此代码吗?或者,如果您认为 cookie 是一个更好的主意,我将如何为这段代码实现 cookie?

var currentSize;
function confSize(){
  if (sessionStorage.getItem('sessionSize') == ""){
      $("#body").css("font-size", "20px");
  }
  else {
      currentSize = sessionStorage.getItem('sessionSize');
      $("#body").css("font-size", currentSize + "px");
      console.log(sessionStorage.getItem('sessionSize'))
  }


    $("#button2").click(function(){
        if (currentSize == 20){
            currentSize = 22;
        }
        else {
            currentSize = currentSize + 2;
        }

        sessionStorage.setItem('sessionSize', currentSize);
        $("#body").css("font-size", currentSize + "px");
    });

    $("#button").click(function(){
        currentSize -= 2;
        sessionStorage.setItem('sessionSize', currentSize);
        $("#body").css("font-size", currentSize + "px");
    });

}

【问题讨论】:

    标签: javascript html cookies web session-storage


    【解决方案1】:

    您需要确保正确使用值作为字符串和适当的整数。如果您执行以下操作:

    currentSize = currentSize + 2
    

    根据currentSize 当前是字符串还是整数,您将获得不同的值。如果它是一个字符串,你会得到你注意到的连接问题。

    现在,您实际上将这个问题与当您将整数 currentSize 值插入 sessionStorage 时,会自动进行整数到字符串转换以进行存储(技术上 .toString() 方法在任何您正在尝试存储以确定要存储的字符串值)。您从 sessionStorage 检索到的内容始终是字符串值,因此您需要在尝试对其进行整数数学运算之前将其转换回整数。

    我建议修改您的代码以明确说明字符串/整数。可能看起来像这样:

    var currentSize;
    function confSize(){
       var defaultSize = 20;
       var sessionStorageStr = sessionStorage.getItem('sessionSize');
       if (sessionStorageStr == ""){
          currentSize = defaultSize;
       }
       else {
          currentSize = parseInt(sessionStorageStr);
          console.log(currentSize);
       }
    
       // no need to repeat this code in both sides of the conditional above
       $("#body").css("font-size", currentSize + "px");
    
    
       $("#button2").click(function(){
            // I removed your conditional here, as it really did nothing        
            currentSize += 2;
            sessionStorage.setItem('sessionSize', currentSize);
            $("#body").css("font-size", currentSize + "px");
        });
    
        $("#button").click(function(){
            currentSize -= 2;
            sessionStorage.setItem('sessionSize', currentSize);
            $("#body").css("font-size", currentSize + "px");
        });
    }
    

    【讨论】:

    • 通过使用 parseInt(),我想?编辑:当我在 2 sessiontorage.getItem 周围使用 parseInt 时(如上所示),按钮不再起作用......你知道那里发生了什么吗?所以:(parseInt(sessionStorage.getItem('sessionSize')) == "")currentSize = parseInt(sessionStorage.getItem('sessionSize'))
    • @Kryptonous 我添加了一些示例代码。您能否详细说明“按钮不再起作用”的说法?
    • 我实现了你的一段代码,我也做了一些修改。现在我按下按钮,但没有任何反应。尽管它在控制台或其他地方没有给出任何错误。真的很奇怪……好像默认值不会改变,但我不认为这是问题所在……我不知道会是什么问题。
    • 您永远不会在您的 OP 中显示您最初如何调用 confSize()。这发生了吗?我还假设currentSize 在全球范围内。是这样吗?您是否尝试进一步调试以查看单击按钮时会发生什么(即单击处理程序是否触发?currentSize 值是您期望的值吗?等等)?
    • $(document).ready(function() { confSize(); }) 这会是问题吗?我会检查处理程序是否触发:编辑:是的,他们会:)
    猜你喜欢
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 2013-01-07
    相关资源
    最近更新 更多