2016-05-14 68 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=/"; 
}); 
相關問題