javascript - if(window.localStorage.checkOutDate==null)这条语句不能执行的原因?
PHPz
PHPz 2017-04-10 15:21:37
[JavaScript讨论组]

我的js代码如下,if后面的语句永远等不到执行,请问怎么破?

<script type="text/javascript"> $(document).ready(function(){ window.localStorage.checkInDate=null; window.localStorage.checkOutDate=null; $("#test1").html(window.localStorage.checkInDate); $("#test2").html(window.localStorage.checkOutDate); $("span").bind("click",function(){ $("#test1").html("hello"); if (window.localStorage.checkInDate==null) { window.localStorage.checkInDate = $(this).attr("title"); $("#test1").html(window.localStorage.checkInDate); } else { window.localStorage.checkOutDate = $(this).attr("title"); $("#test2").html(window.localStorage.checkOutDate); } }); }); </script>
PHPz
PHPz

学习是最好的投资!

全部回复(1)
迷茫

checkInDate 和checkOutDate 都是localStorage对象上用户自定义的属性。这些属性虽然可以被赋值为一个对象,但是实际存储都是字符串,会调用对象的适当方法(toString或者valueOf)进行转换。

 var a = {};  
 window.localStorage.checkInDate = a;   
 window.localStorage.checkOutDate = null;                   
 console.log(window.localStorage.checkInDate);  // "[Ojbect Ojbect]"
 console.log(window.localStorage.checkOutDate); // "null"   
 console.log(typeof (window.localStorage.checkInDate));  // string
 console.log(typeof (window.localStorage.checkOutDate)); // string

所以这句 if(window.localStorage.checkOutDate==null) 等价于 if ("null" == null) . 是不可能成立的。
另外建议楼主以后贴代码的时候,最好格式化。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号