jQuery.只显示一次

jQuery .one only show once

本文关键字:一次 显示 jQuery      更新时间:2023-09-26

我们有一个模式对话框,当用户离开浏览器主体时会激活。这是使用下面的.one()代码。不是吗。一个应该显示模态一次吗?有没有可能每个用户只做一次(cookie?),这样每次他们回到页面时,当他们离开身体时就不会重复了?

jQuery("body").one('mouseleave', function() {
    jQuery("#modal-background").toggleClass("active");
    jQuery("#modal-content").toggleClass("active");
});

有什么建议吗?

检查函数中cookie的值,如果值不为1,则表示模式对话框尚未显示,它将进入if并显示模式对话框,并将值设置为1。下一次cookie为1时,它永远不会进入if内部,因此它永远不会显示模式对话框。

jQuery("body").one('mouseleave', function() {
   if(jQuery.cookie("dialogDisplayed") !==1){
     jQuery("#modal-background").toggleClass("active");
     jQuery("#modal-content").toggleClass("active");
     jQuery.cookie("dialogDisplayed", '1', { expires: 30 });
   }
});