保存事件onclick js

时间:2017-10-12 06:59:37

标签: javascript jquery

我有这段代码

<script>
    var content = document.getElementById('float_content_right');
    var hide = document.getElementById('hide_float_right');
    function hide_float_right() {

        if (content.style.display == "none") {
            content.style.display = "block";
            hide.innerHTML = '<a href="javascript:hide_float_right()">Close [X]</a>';

        } else {
            content.style.display = "none";
            hide.innerHTML = '<a href="javascript:hide_float_right()">Open...</a>';
        }
    }
    var images = new Array();
    images[0] = "<a href='http://phuongnuochoa.com' target='_blank'><img src='http://thegioidoco.net/gif/1.gif'></a>";
    images[1] = "<a href='http://quattran.com' target='_blank'><img src='http://thegioidoco.net/gif/2.gif'></a>";
    images[2] = "<a href='http://dogoducthien.vn' target='_blank'><img src='http://thegioidoco.net/gif/3.gif'></a>";

    $(images[Math.floor(Math.random() * images.length)]).appendTo('#float_content_right');
    setInterval(function () {
        $('#float_content_right').empty();
        $(images[Math.floor(Math.random() * images.length)]).appendTo('#float_content_right');
    }, 5000);
</script>

现在我想要, 当我按下关闭按钮时图像被隐藏,然后再次F5图像仍然被隐藏。如果我没有按下关闭按钮,那么当我F5时,图像仍然存在。

请帮助我,这是我的运动!

感谢。

1 个答案:

答案 0 :(得分:0)

在代码中使用会话存储。 Like this

单击“关闭”按钮,在会话存储中设置密钥。

// Save data to sessionStorage
sessionStorage.setItem('imageVisible', '1');

在F5或页面加载函数调用(通常为$(document).ready(function(){}))上,您可以检查此内容。

// Get saved data from sessionStorage
var data = sessionStorage.getItem('imageVisible');
if(data == 1)
{
  //show the div containing the image.
}
else
{
  //hide the div containing the image.
}
相关问题