如何根据' if'隐藏提醒?条件

时间:2015-05-17 20:45:11

标签: javascript jquery

我会在一年中的特定日期在网站上显示警报。

我的问题是:如果日期不属于其中任何一天,我该如何隐藏该警报?我认为下面的代码可以工作,但它只根据我能看到的代码中的条件延迟警报,但我不知道如何解决这个问题。

$(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);
});

3 个答案:

答案 0 :(得分:0)

不确定 $rootScope.stepText = gettextCatalog.getString("step_1_header"); 来自哪里,但应该是if (month == 0, date ==1)。对于每个if (month == 0 && date == 1) s

,这应该是相同的

答案 1 :(得分: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
}

答案 2 :(得分:0)

我正在尝试学习JQuery,所以我不知道如何解决上述问题,但我想我终于做到了!我在最后的IF声明之后添加了下面的代码。

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