2015-05-17 19 views
0

我有一個提醒,顯示在網站上,在一年中的特定日子。如何隱藏基於'如果'條件的提醒

我的問題是這樣的:如果日期不是那些日子之一,我該如何隱藏該警報?我在考慮下面的代碼可以工作,但它只會延遲基於我可以看到的代碼中的條件的警報,但我不知道如何解決該問題。

$(document).ready(function() { 
    /* if (typeof window.sessionStorage != undefined) { 
     if (!sessionStorage.getItem('mySessionVal')) { */ 
      $('<div />', { 
       id : "alertmsg", 
       text :"", 
       on : { 
        click: function() { 
         $(this).slideUp(500); 
        } 
       } 
      }).appendTo('body').slideDown(1).delay(6000).slideToggle("fast"); 
      sessionStorage.setItem('mySessionVal', false); 
      sessionStorage.setItem('storedWhen', (new Date()).getTime()); 

     var now = new Date(); 
     var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; 
     var month = now.getMonth(); 
     var date = now.getDate(); 
     var year = now.getFullYear(); 
     var msg; 
     if (month == 0, date ==1) msg = "Submissions for our January issue have now closed."; 
     else if (month == 3, date ==1) msg = "Submissions for our April issue have now closed."; 
     else if (month == 6, date ==1) msg = "Submissions for our July issue have now closed."; 
     else if (month == 9, date ==1) msg = "Submissions for our October issue have now closed."; 
     else $('#alertmsg').hide(); 
     $('#alertmsg').text(msg); 
}); 

回答

0

不知道你在哪裏得到了if (month == 0, date ==1)但它應該是if (month == 0 && date == 1)。這應該是相同的每個if小號

0

你的最後一行之後的if語句是

$('#alertmsg').text(msg); 

這意味着你檢查月份名稱,無論到msg內容後,你把文本在div裏面。

你可以做這樣的事情:

var now = new Date(); 
     var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; 
     var month = now.getMonth(); 
     var date = now.getDate(); 
     var year = now.getFullYear(); 
     var msg; 
     if (month == 0, date ==1) msg = "Submissions for our January issue have now closed."; 
     else if (month == 3, date ==1) msg = "Submissions for our April issue have now closed."; 
     else if (month == 6, date ==1) msg = "Submissions for our July issue have now closed."; 
     else if (month == 9, date ==1) msg = "Submissions for our October issue have now closed."; 

if(msg != '') { 

// append the div to body here and assign the msg into it 
} 
+0

如果日期不符合上述任何一天,我根本不需要任何消息。將追加分區隱藏它們在一起? – Marlon

+0

你只會在msg包含內容時追加div,所以如果日期不是你的if語句之一,則msg將爲空,並且div不會被創建並附加到主體 – Lupin

0

我想學習jQuery,所以我並沒有對如何解決上述問題的一個好主意,但我想我終於做到了!我在最後的IF語句後添加了下面的代碼。

else $('#alertmsg').removeAttr('id'); 
     $('#alertmsg').text(msg);