且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当用户关闭< div>时,请隐藏< div>在所有网站页面上

更新时间:2023-02-06 17:35:14

使用 localStorage()

本地存储是按来源(每个域和协议)


  • 点击DIV关闭后,您可以获得当前时间戳

  • 添加小时数(24)那个时间戳

  • 将该值存储在 localStorage 中,作为 localStorage.setItem('desiredTime',time)

  • 使用存储的时间戳检查当前时间戳 localStorage.getItem('desiredTime') ,基于显示/隐藏

  • On click of DIV close, you can get the current time-stamp
  • Add number of hours (24) to that time-stamp
  • Store that value in localStorage as localStorage.setItem('desiredTime', time)
  • Check current time-stamp with that stored time-stamp localStorage.getItem('desiredTime'), based on that show/hide

jQuery

$(document).ready(function(){
        //Get current time
        var currentTime = new Date().getTime();
        //Add hours function
        Date.prototype.addHours = function(h) {    
           this.setTime(this.getTime() + (h*60*60*1000)); 
           return this;   
        }
        //Get time after 24 hours
        var after24 = new Date().addHours(10).getTime();
        //Hide div click
        $('.hide24').click(function(){
            //Hide div
            $(this).hide();
            //Set desired time till you want to hide that div
            localStorage.setItem('desiredTime', after24); 
        });
        //If desired time >= currentTime, based on that HIDE / SHOW
        if(localStorage.getItem('desiredTime') >= currentTime)
        {
            $('.hide24').hide();
        }
        else
        {
            $('.hide24').show();
        }
});

HTML

<div>DIV-1</div>
<div class='hide24'>DIV-2</div>

注意事项


  • 您也可以使用 $ .cookie ,但现在这是一种较旧的方法。

  • < div> ,类 hide24 将仅隐藏。

  • 确保将此代码放在通用JavaScript中,该JavaScript会加载到您网站的每个HTTP请求上。

  • 对于 localStorage ,您应该有HTML5浏览器。

  • You can use $.cookie as well, but that's an older approach now.
  • <div> with class hide24 will be hidden only.
  • Make sure that you put this code in general JavaScript, which loads on every HTTP request of your website.
  • For localStorage, you should have HTML5 browsers.

网络存储HTML5

希望这会有所帮助。