问题描述
以下简单代码适用于Firefox(12.0),但在IE 9中似乎不起作用,即使IE9支持本地存储。请注意alert(localStorage.lastname);没有显示任何结果。想知道在IE9中使用localStorage是否存在已知问题,因为文档确实说它受支持。
The following simple code works in Firefox (12.0) but does not seem to work in IE 9 even though local storage is supported in IE9. Notice how alert(localStorage.lastname); does not show up any results. Was wondering if there is a known issue in using localStorage in IE9 as the documentation does say it is supported.
<!DOCTYPE html>
<html>
<body>
<script>
if(typeof(Storage)!=="undefined")
{
alert('local storage')
localStorage.lastname="Smith";
alert(localStorage.lastname);
}
else
{
alert("Sorry, your browser does not support web storage...")
}
</script>
</body>
</html>
推荐答案
切勿直接在localstorage中设置/获取项目!使用适当的方法:
Never set/get the items in localstorage directly! Use the appropriate methods for that:
localStorage.setItem(key,value)
localStorage.getItem(key)
localStorage.removeItem(key)
这可以修复你的IE问题,你会幸福地生活: - D
This fixes your IE problem and you will live happily :-D
(注意,这些值是作为字符串存储的!)
(Note, that the values are stores as strings!)
这篇关于HTML5代码在IE9中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!