• <tfoot id='z81hQ'></tfoot>

    1. <legend id='z81hQ'><style id='z81hQ'><dir id='z81hQ'><q id='z81hQ'></q></dir></style></legend>
      <i id='z81hQ'><tr id='z81hQ'><dt id='z81hQ'><q id='z81hQ'><span id='z81hQ'><b id='z81hQ'><form id='z81hQ'><ins id='z81hQ'></ins><ul id='z81hQ'></ul><sub id='z81hQ'></sub></form><legend id='z81hQ'></legend><bdo id='z81hQ'><pre id='z81hQ'><center id='z81hQ'></center></pre></bdo></b><th id='z81hQ'></th></span></q></dt></tr></i><div id='z81hQ'><tfoot id='z81hQ'></tfoot><dl id='z81hQ'><fieldset id='z81hQ'></fieldset></dl></div>
      • <bdo id='z81hQ'></bdo><ul id='z81hQ'></ul>

      <small id='z81hQ'></small><noframes id='z81hQ'>

      1. 如何使 localStorage 值为 true?

        时间:2023-09-04
        <tfoot id='02wlZ'></tfoot>

          <bdo id='02wlZ'></bdo><ul id='02wlZ'></ul>
          <i id='02wlZ'><tr id='02wlZ'><dt id='02wlZ'><q id='02wlZ'><span id='02wlZ'><b id='02wlZ'><form id='02wlZ'><ins id='02wlZ'></ins><ul id='02wlZ'></ul><sub id='02wlZ'></sub></form><legend id='02wlZ'></legend><bdo id='02wlZ'><pre id='02wlZ'><center id='02wlZ'></center></pre></bdo></b><th id='02wlZ'></th></span></q></dt></tr></i><div id='02wlZ'><tfoot id='02wlZ'></tfoot><dl id='02wlZ'><fieldset id='02wlZ'></fieldset></dl></div>
        • <legend id='02wlZ'><style id='02wlZ'><dir id='02wlZ'><q id='02wlZ'></q></dir></style></legend>

                <small id='02wlZ'></small><noframes id='02wlZ'>

                    <tbody id='02wlZ'></tbody>

                  本文介绍了如何使 localStorage 值为 true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道 localStorage 是否有可能使用布尔值而不是字符串?

                  I was wondering if its possible for localStorage to have a Boolean value instead of a string?

                  如果不可能或可以在 JS 中以不同的方式完成,请仅使用 JS 不使用 JSON,请告诉我,谢谢

                  Using JS only no JSON if its impossible or can be done in JS a different way please let me know thanks

                  http://jsbin.com/qiratuloqa/1/

                  //How to set localStorage "test" to true?
                  
                  test = localStorage.getItem("test");
                  localStorage.setItem("test", true); 
                  
                  if (test === true) {
                    alert("works");
                  } else {
                    alert("Broken");
                  }
                  
                  
                  
                  /* String works fine.
                  
                  test = localStorage.getItem("test");
                  localStorage.setItem("test", "hello"); 
                  
                  if (test === "hello") {
                    alert("works");
                  } else {
                    alert("Broken");
                  }
                  
                  */
                  

                  推荐答案

                  我想知道 localStorage 是否可以使用布尔值而不是字符串?

                  I was wondering if its possible for localStorage to have a Boolean value instead of a string?

                  不,网络存储 只存储字符串.为了存储更丰富的数据,人们通常在存储时使用 JSON 和 stringify,在检索时使用解析.

                  No, web storage only stores strings. To store more rich data, people typically use JSON and stringify when storing and parse when retrieving.

                  存储:

                  var test = true;
                  localStorage.setItem("test", JSON.stringify(test)); 
                  

                  检索:

                  test = JSON.parse(localStorage.getItem("test"));
                  console.log(typeof test); // "boolean"
                  

                  不过,您不需要 JSON 作为布尔值;您可以只使用 "" 表示 false 和任何其他字符串表示 true,因为 "" 是一个falsey"值(当被视为布尔值时强制为 false 的值).

                  You don't need JSON for just a boolean, though; you could just use "" for false and any other string for true, since "" is a "falsey" value (a value that coerces to false when treated as a boolean).

                  这篇关于如何使 localStorage 值为 true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:localStorage 和布尔“字符串" 下一篇:Javascript &amp;&amp;运算符 as if 语句?

                  相关文章

                    <legend id='33JOP'><style id='33JOP'><dir id='33JOP'><q id='33JOP'></q></dir></style></legend>

                    <small id='33JOP'></small><noframes id='33JOP'>

                      <bdo id='33JOP'></bdo><ul id='33JOP'></ul>
                  1. <tfoot id='33JOP'></tfoot>

                      <i id='33JOP'><tr id='33JOP'><dt id='33JOP'><q id='33JOP'><span id='33JOP'><b id='33JOP'><form id='33JOP'><ins id='33JOP'></ins><ul id='33JOP'></ul><sub id='33JOP'></sub></form><legend id='33JOP'></legend><bdo id='33JOP'><pre id='33JOP'><center id='33JOP'></center></pre></bdo></b><th id='33JOP'></th></span></q></dt></tr></i><div id='33JOP'><tfoot id='33JOP'></tfoot><dl id='33JOP'><fieldset id='33JOP'></fieldset></dl></div>