2016-05-14 67 views
1

我已经为打开的弹出框编写了这段代码。代码如下所述。我已经设置了cookie,以便它只打开一次。我有问题,而我点击关闭按钮cookie没有设置为弹出,以便它重新打开每一次。当点击弹出框的关闭按钮时cookie没有被设置

<div id="abcPopup"> 
    <div id="popup-title"> 
     Hello 
    </div> 
    <div id="description"> 

    </div> 
    <div id="picture"> 

    </div> 

    <span style="display: none;" id="notification"></span>   
</div> 

我的jQuery代码如下所示

$(document).ready(function() { 
     var pageVisitedcookiesValue = getCookie("page_visited"); 
     if (pageVisitedcookiesValue == null || pageVisitedcookiesValue != "true") { 
      var pageVisited; 
      setTimeout(function() { 
       $("#abcPopup").dialog({ 
        show: { effect: 'fade', duration: 450 }, 
        hide: { effect: 'fade', duration: 450 } 
       }); 
      }, 3000);    
      $('.ui-button-icon-only').click(function() { 

       pageVisited = true; 
       document.cookie = "page_visited" + "=" + pageVisited + ";" + "path=/"; 
      }); 
     } 
    }); 

function getCookie(c_name) { 
     var c_value = document.cookie; 
     var c_start = c_value.indexOf(" " + c_name + "="); 
     if (c_start == -1) { 
      c_start = c_value.indexOf(c_name + "="); 
     } 
     if (c_start == -1) { 
      c_value = null; 
     } 
     else { 
      c_start = c_value.indexOf("=", c_start) + 1; 
      var c_end = c_value.indexOf(";", c_start); 
      if (c_end == -1) { 
       c_end = c_value.length; 
      } 
      c_value = unescape(c_value.substring(c_start, c_end)); 
     } 
     return c_value; 
    } 

现在我ahve而我设置超时时间为打开的对话框中我的onclick事件是不是同时关闭按钮点击关闭弹出框捕捉只会出现问题。

我已经把警报框的相同,但其按钮点击甚至没有火灾或得到如此警觉发生,但虽然我会删除设置超时功能我的弹出窗口工作正常。即使是关闭按钮上的警报也是如此。

请为相同的指导。

回答

2

您正在立即绑定click事件,但使用setTimeout来创建对话框。因此,绑定处理程序时没有.ui-button-icon-only元素。

一个解决方案是立即创建对话框,但使用autoOpen: false,然后在setTimeout中打开对话框。

$(document).ready(function() { 
    var pageVisitedcookiesValue = getCookie("page_visited"); 
    if (pageVisitedcookiesValue == null || pageVisitedcookiesValue != "true") { 
     var pageVisited; 
     $("#abcPopup").dialog({ 
      autoOpen: false, 
      show: { effect: 'fade', duration: 450 }, 
      hide: { effect: 'fade', duration: 450 } 
     }); 
     setTimeout(function() { 
      $("#abcPopup").dialog("open"); 
     }, 3000);    
     $('.ui-button-icon-only').click(function() { 

      pageVisited = true; 
      document.cookie = "page_visited" + "=" + pageVisited + ";" + "path=/"; 
     }); 
    } 
}); 

另一种解决方案是使用事件委托。

$(document).on("click", ".ui-button-icon-only", function() { 
    pageVisited = true; 
    document.cookie = "page_visited" + "=" + pageVisited + ";" + "path=/"; 
}); 
相关问题