简体   繁体   English

jQuery .one只显示一次

[英]jQuery .one only show once

We have a modal dialog which activates when the user goes outside the browser body. 我们有一个模式对话框,当用户离开浏览器主体时会激活。 This is using the .one() code below. 这是使用下面的.one()代码。 Isn't .one supposed to show the modal once? 不应该一次显示模态吗? Is it possible to do this only once per user (cookie?) so every time they go back to the page it's not repeated when they go outside the body? 每个用户是否只能执行一次此操作(cookie?),所以每次他们回到页面时,当他们离开身体时就不会重复吗?

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

Any suggestions? 有什么建议么?

check the cookie's value inside the function, if the value is not 1, it means that the modal dialog hasn't shown yet and it goes inside the if and shows the modal dialog and set value to 1. Then next time the cookie is 1 and it never goes inside the if, so it never shows the modal dialog. 检查函数中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 });
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM