如何使模式窗口只显示一次

How do I make my modal window only show up once?

本文关键字:一次 显示 何使 模式 窗口      更新时间:2023-09-26

如何在脚本中添加cookie附件?我浏览了这个网站,试图放置代码,但都没有成功。

这是我的代码:

$(document).ready(function() {  
//select all the a tag with name equal to modal
$('a[name=modal]').click(function(e) {
    //Cancel the link behavior
    e.preventDefault();
    //Get the A tag
    var id = $(this).attr('href');
    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    //Set height and width to mask to fill up the whole screen
    $('#mask').css({'width':maskWidth,'height':maskHeight});
    //transition effect     
    $('#mask').fadeIn(1000);    
    $('#mask').fadeTo("slow",0.8);  
    //Get the window height and width
    var winH = $(window).height();
    var winW = $(window).width();
    //Set the popup window to center
    $(id).css('top',  winH/2-$(id).height()/2);
    $(id).css('left', winW/2-$(id).width()/2);
    //transition effect
    $(id).fadeIn(2000); 
});
//if close button is clicked
$('.window .close').click(function (e) {
    //Cancel the link behavior
    e.preventDefault();
    $('#mask, .window').hide();
});     
//if mask is clicked
$('#mask').click(function () {

任何人都可以帮我添加一个cookie片段,这样窗口只打开一次/用户,那就太好了。

使用one():只能在click上附加一次事件

$('a[name=modal]').one('click', function(){ ... });