简体   繁体   English

根据一天中的小时更改jQuery的div

[英]change div depending on hour of day with jQuery

Anyone who wants to take a stab at this, I could really use your help! 任何想对此采取行动的人,我真的可以使用您的帮助!

I have a couple lines of php, that switches out a js file depending on the day of the week. 我有几行php,根据星期几切换出js文件。 in each js file, I need it to hide/show a div depending on the hour. 在每个js文件中,我需要根据小时来隐藏/显示div。 For some reason this isn't working. 由于某种原因,这不起作用。 any ideas? 有任何想法吗?

$(document).ready(function () {

//initialize on-air functions for Sunday... Yay it's Jesus day!
    var d = new Date();
    var currentHour = d.getHours(); //0-23

    if (currentHour > 5 && currentHour < 6) 
     { 
         $('#no_onair').hide();
         $('#newdimesion_onair').show();


      }
      else if (currentHour > 6 && currentHour < 7) 
     { 
         $('#no_onair').hide();
         $('#artfthesong_onair').show();


      }
      else if (currentHour > 8 && currentHour < 10) 
     { 
         $('#no_onair').hide();
         $('#thejazzshow_onair').show();


      }

    else if (currentHour > 11 && currentHour < 13) 
     { 
         $('#no_onair').hide();
         $('#rob_onair').show();


      }


      else if (currentHour > 13 && currentHour < 14) 
     { 
         $('#no_onair').hide();
         $('#nick_onair').show();


      }

       else if (currentHour > 16 && currentHour < 18) 
     { 
         $('#no_onair').hide();
         $('#stadler_onair').show();



      }
    else {
       $('#no_onair').show();
       }

setTimeout(function() {
     $('#fixed_onair').addClass('animated fadeInDown');
     $('#fixed_onair').show();
},  5100);

});

i am not sure why you would use php to swap JS files. 我不确定为什么要用php交换JS文件。 The easiest way to do this is to assign CSS classes to your divs and then just use jQuery to hide/show. 最简单的方法是将CSS类分配给div,然后仅使用jQuery隐藏/显示。

Below is a snippet that works. 以下是有效的代码段。 This will display the correct div on load. 这将在加载时显示正确的div。 If you want to swap them out dynamically for people already on the site you can set a timer that checks every xx minutes and run the same code. 如果要动态地将它们换成网站上已有的人员,则可以设置一个计时器,每隔xx分钟检查一次,并运行相同的代码。 The each loop can probably be optimized if need be, but does not seem you have that many divs. 如果需要,可能可以优化每个循环,但似乎没有那么多div。 either way, much better than using PHP to inject scripts. 无论哪种方式,都比使用PHP注入脚本好得多。

 <div class="all d0 h1 h2 h3 h4" style="display:none;"> Sunday 1, 2, 3, 4 </div> <div class="all d0 h5 h6 h7 h18" style="display:none;"> Sunday 5, 6, 7, 18 </div> <div class="all d1 h1 h2 h3 h4" style="display:none;"> Monday 1, 2, 3, 4 </div> <div class="all d1 h5 h6 h7 h8" style="display:none;"> Monday 5, 6, 7, 8 </div> <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script> <script> $(document).ready(function() { $("div.all").hide(); var date = new Date(); var d = date.getDay(); var h = date.getHours(); console.log('searching for ' + d + ' and ' + h); $("div.all").each(function() { if ( $(this).hasClass("d" + d) && $(this).hasClass("h"+h) ) { $(this).show(); } }); }); </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM