您好,我有一个本地存储链接,需要显示为 img src。如果本地存储链接是 google.com 我希望它显示为 img src到目前为止的代码<body> <div id="result"> </div> <img src="" id="photo" /> //this is the img element <script> if (typeof(Storage) !== "undefined") { var name = localStorage.getItem('name'); var dataImage = localStorage.getItem('imgData'); document.getElementById("photo").innerHTML = localStorage.getItem("name"); //localstorage } else { document.getElementById("photo").innerHTML = "Sorry, your browser does not support Web Storage..."; } </script></body>我知道这个问题很令人困惑,但这是我能描述的最好的。
1 回答
凤凰求蛊
TA贡献1825条经验 获得超4个赞
您可以将图像src属性设置为从本地存储获取的值。(假设这是图像的路径)
您不想设置innerHTML标签的img。相反,使用另一个元素来显示文本。
<div id="result"></div>
<img src="" id="photo" /> //this is the img element
<div id="msg"></div>
<script>
if (typeof(Storage) !== "undefined") {
var name = localStorage.getItem('name');
var dataImage = localStorage.getItem('imgData');
var imageName = localStorage.getItem("name");
var imageEl = document.getElementById("photo");
imageEl.src = imageName
} else {
document.getElementById("msg").innerHTML = "Sorry, your browser does not support Web Storage...";
}
</script>
添加回答
举报
0/150
提交
取消