简体   繁体   English

使用JavaScript显示图像

[英]using javascript to display an image

I have an image as follow: 我有一个图像如下:
<img id="imgId" src="img/cart.png" style="display: none"/>
When a button is clicked, it calls a JavaScript function to display the image 单击按钮时,它将调用JavaScript函数以显示图像

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

The image is displayed well but it does not show up when I go back from another page. 图像显示良好,但是当我从另一页返回时却没有显示。
What I want is after displaying the image, it will be still there even I go to another page and back. 我想要的是在显示图像后,即使我转到另一页并返回,该图像仍会存在。
Any helps are appreciated. 任何帮助表示赞赏。

You could do it by saving the image "state" in the URL-hash of the page. 您可以通过将图像“状态”保存在页面的URL哈希中来实现。 (The thing after # ). (#之后的东西)。

When displaying the image, do it like this: 显示图像时,请按照以下步骤操作:

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

And on pageload you run this little piece of code. 然后在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