繁体   English   中英

使用JavaScript显示图像

[英]using javascript to display an image

我有一个图像如下:
<img id="imgId" src="img/cart.png" style="display: none"/>
单击按钮时,它将调用JavaScript函数以显示图像

document.getElementById("imgId").style.display = "inline"

图像显示良好,但是当我从另一页返回时却没有显示。
我想要的是在显示图像后,即使我转到另一页并返回,该图像仍会存在。
任何帮助表示赞赏。

您可以通过将图像“状态”保存在页面的URL哈希中来实现。 (#之后的东西)。

显示图像时,请按照以下步骤操作:

document.getElementById("imgId").style.display = "inline";
window.location.hash = "imgIdShown";

然后在pageload上运行这段小代码。

if (window.location.hash == "imgIdShown")
{
    document.getElementById("imgId").style.display = "inline";
}

如果可能,将您的状态保存在会话中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM