dongyanhu5628 2017-01-02 22:52
浏览 97
已采纳

在重新加载页面上保持jquery打开之前隐藏的div

I have two divs. One acts as a chat window launcher. Chat window element is hidden by default. When I click the chat window launcher element, chat window appears and launcher hides. I've also made a small close button for the chat window that reverses the operation.

The problem is that when I reload the page, the chat window disappears and I have to relaunch again. What can I do, so that once the launcher is activated, the chat window remains visible even on page reload so that I can only close it using its close button?

HTML

<div class="chat-launcher" title="Launch Chat Window">
    <span class=" glyphicon glyphicon-pencil chat-launcher-pencil " style="color: #ffffff;font-size: 20px;"></span>
</div>
<div class="chat-window ">
    <span id="close">X</span>
</div>

jQuery

<script type="text/javascript">
    $(document).ready(function () {
        $('.chat-launcher').on('click', function () {
            $('.chat-window').show();
            $('.chat-launcher').hide();
        });
    });

    $(document).ready(function(){
        $('#close').on('click',function(){
            $('.chat-window').hide();
            $('.chat-launcher').show();
        });
    });
</script>
  • 写回答

1条回答 默认 最新

  • duanbi3151 2017-01-03 00:26
    关注

    You need a persistent storage in order to save the chat window's state; like server-side sessions, client-side cookies or localStorage.

    Utilizing localStorage, it will be something along the lines of:

    $(document).ready(function () {
        $('.chat-launcher').on('click', function () {
            $('.chat-window').show();
            $('.chat-launcher').hide();
    
            // Set a flag indicating that the char window is open
            localStorage.setItem('chatWindowOpen', true);
        });
    
        $('#close').on('click',function(){
            $('.chat-window').hide();
            $('.chat-launcher').show();
    
            // Remove the flag
            localStorage.removeItem('chatWindowOpen');
        });
    
        // Open chat window if the flag is there
        if (localStorage.getItem('chatWindowOpen')) {
            $('.chat-launcher').trigger('click');    
        }        
    });
    

    One other note; As you might have noticed, you don't need multiple $(document).ready() calls. One will do for all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?