我的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>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
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) . 是不可能成立的。
另外建议楼主以后贴代码的时候,最好格式化。