我在我的网站中添加了一个查询,以便根据日期显示div。我在我的网站上添加了30个div,并向他们添加了display none。使用基于日期的查询使每天激活一个div。以前,当我添加2天时,它正在工作。但是,当我在30天的基础上添加它时,它对我不起作用。我的网址是covered query。
您可以检查代码是否已执行here。
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
window.setInterval(function(){
var current = new Date();
var expiry = new Date("March 26, 2017 12:00:00")
var expiry2 = new Date("March 27, 2017 12:00:00")
var expiry3 = new Date("March 28, 2017 12:00:00")
var expiry4 = new Date("March 29, 2017 12:00:00")
var expiry5 = new Date("March 30, 2017 12:00:00")
var expiry6 = new Date("April 01, 2017 12:00:00")
var expiry7 = new Date("April 02, 2017 12:00:00")
var expiry8 = new Date("April 03, 2017 12:00:00")
var expiry9 = new Date("April 04, 2017 12:00:00")
var expiry10 = new Date("April 05, 2017 12:00:00")
var expiry11 = new Date("April 06, 2017 12:00:00")
var expiry12 = new Date("April 07, 2017 12:00:00")
var expiry13 = new Date("April 08, 2017 12:00:00")
var expiry14 = new Date("April 09, 2017 12:00:00")
var expiry15 = new Date("April 10, 2017 12:00:00")
var expiry16 = new Date("April 11, 2017 12:00:00")
var expiry17 = new Date("April 12, 2017 12:00:00")
var expiry18 = new Date("April 13, 2017 12:00:00")
var expiry19 = new Date("April 14, 2017 12:00:00")
var expiry20 = new Date("April 15, 2017 12:00:00")
var expiry21 = new Date("April 16, 2017 12:00:00")
var expiry22 = new Date("April 17, 2017 12:00:00")
var expiry23 = new Date("April 18, 2017 12:00:00")
var expiry24 = new Date("April 19, 2017 12:00:00")
var expiry25 = new Date("April 20, 2017 12:00:00")
var expiry26 = new Date("April 21, 2017 12:00:00")
var expiry27 = new Date("April 22, 2017 12:00:00")
var expiry28 = new Date("April 23, 2017 12:00:00")
var expiry29 = new Date("April 24, 2017 12:00:00")
var expiry30 = new Date("April 25, 2017 12:00:00")
if(current.getTime()>expiry.getTime()){
$('.panchang_one').hide();
$('.panchang_two').show();
}
else if(current.getTime()>expiry2.getTime()){
$('.panchang_two').hide();
$('.panchang_three').show();
}
else if(current.getTime()>expiry3.getTime()){
$('.panchang_three').hide();
$('.panchang_four').show();
}
else if(current.getTime()>expiry4.getTime()){
$('.panchang_four').hide();
$('.panchang_five').show();
}
else if(current.getTime()>expiry5.getTime()){
$('.panchang_five').hide();
$('.panchang_six').show();
}
else if(current.getTime()>expiry6.getTime()){
$('.panchang_six').hide();
$('.panchang_seven').show();
}
else if(current.getTime()>expiry7.getTime()){
$('.panchang_seven').hide();
$('.panchang_eight').show();
}
else if(current.getTime()>expiry8.getTime()){
$('.panchang_eight').hide();
$('.panchang_nine').show();
}
else if(current.getTime()>expiry9.getTime()){
$('.panchang_nine').hide();
$('.panchang_ten').show();
}
else if(current.getTime()>expiry10.getTime()){
$('.panchang_ten').hide();
$('.panchang_eleven').show();
}
else if(current.getTime()>expiry11.getTime()){
$('.panchang_eleven').hide();
$('.panchang_twelve').show();
}
else if(current.getTime()>expiry12.getTime()){
$('.panchang_twelve').hide();
$('.panchang_thirteen').show();
}
else if(current.getTime()>expiry13.getTime()){
$('.panchang_thirteen').hide();
$('.panchang_fourteen').show();
}
else if(current.getTime()>expiry14.getTime()){
$('.panchang_fourteen).hide();
$('.panchang_fifteen').show();
}
else if(current.getTime()>expiry15.getTime()){
$('.panchang_fifteen').hide();
$('.panchang_sixteen').show();
}
else if(current.getTime()>expiry16.getTime()){
$('.panchang_sixteen').hide();
$('.panchang_seventeen').show();
}
else if(current.getTime()>expiry17.getTime()){
$('.panchang_seventeen').hide();
$('.panchang_eighteen').show();
}
else if(current.getTime()>expiry18.getTime()){
$('.panchang_eighteen').hide();
$('.panchang_nineteen').show();
}
else if(current.getTime()>expiry19.getTime()){
$('.panchang_nineteen').hide();
$('.panchang_twenty').show();
}
else if(current.getTime()>expiry20.getTime()){
$('.panchang_twenty').hide();
$('.panchang_twentyone').show();
}
else if(current.getTime()>expiry21.getTime()){
$('.panchang_twentyone').hide();
$('.panchang_twentytwo').show();
}
else if(current.getTime()>expiry22.getTime()){
$('.panchang_twentytwo').hide();
$('.panchang_twentythree').show();
}
else if(current.getTime()>expiry23.getTime()){
$('.panchang_twentythree').hide();
$('.panchang_twentyfour').show();
}
else if(current.getTime()>expiry24.getTime()){
$('.panchang_twentyfour').hide();
$('.panchang_twentyfive').show();
}
else if(current.getTime()>expiry25.getTime()){
$('.panchang_twentyfive').hide();
$('.panchang_twentysix').show();
}
else if(current.getTime()>expiry26.getTime()){
$('.panchang_twentysix').hide();
$('.panchang_twentyseven').show();
}
else if(current.getTime()>expiry27.getTime()){
$('.panchang_twentyseven').hide();
$('.panchang_twentyeight').show();
}
else if(current.getTime()>expiry28.getTime()){
$('.panchang_twentyeight').hide();
$('.panchang_twentynine').show();
}
else if(current.getTime()>expiry29.getTime()){
$('.panchang_twentynine').hide();
$('.panchang_thirty').show();
}
}, 3000);
$('.panchang_one').show();
</script>
有谁能指出我做错了什么?
答案 0 :(得分:0)
你的javascript逻辑存在问题。在您的情况下,当前日期始终大于任何到期命名变量中的日期,因此始终首先执行 if语句,其余语句在其他中提供块永远不会被执行。这就是if elseif语句的工作方式,即当if块中的条件求值为true时,则不执行elseif语句。 您需要检查所有到期日变量的当前日期,即有效期,到期时间2,到期时间3,到期时间等等。
需要更正: 将 else if 替换为 if 。
这是正确的逻辑:
<script>
window.setInterval(function(){
var current = new Date();
var expiry = new Date("March 26, 2017 12:00:00")
var expiry2 = new Date("March 27, 2017 12:00:00")
var expiry3 = new Date("March 28, 2017 12:00:00")
var expiry4 = new Date("March 29, 2017 12:00:00")
var expiry5 = new Date("March 30, 2017 12:00:00")
var expiry6 = new Date("April 01, 2017 12:00:00")
var expiry7 = new Date("April 02, 2017 12:00:00")
var expiry8 = new Date("April 03, 2017 12:00:00")
var expiry9 = new Date("April 04, 2017 12:00:00")
var expiry10 = new Date("April 05, 2017 12:00:00")
var expiry11 = new Date("April 06, 2017 12:00:00")
var expiry12 = new Date("April 07, 2017 12:00:00")
var expiry13 = new Date("April 08, 2017 12:00:00")
var expiry14 = new Date("April 09, 2017 12:00:00")
var expiry15 = new Date("April 10, 2017 12:00:00")
var expiry16 = new Date("April 11, 2017 12:00:00")
var expiry17 = new Date("April 12, 2017 12:00:00")
var expiry18 = new Date("April 13, 2017 12:00:00")
var expiry19 = new Date("April 14, 2017 12:00:00")
var expiry20 = new Date("April 15, 2017 12:00:00")
var expiry21 = new Date("April 16, 2017 12:00:00")
var expiry22 = new Date("April 17, 2017 12:00:00")
var expiry23 = new Date("April 18, 2017 12:00:00")
var expiry24 = new Date("April 19, 2017 12:00:00")
var expiry25 = new Date("April 20, 2017 12:00:00")
var expiry26 = new Date("April 21, 2017 12:00:00")
var expiry27 = new Date("April 22, 2017 12:00:00")
var expiry28 = new Date("April 23, 2017 12:00:00")
var expiry29 = new Date("April 24, 2017 12:00:00")
var expiry30 = new Date("April 25, 2017 12:00:00")
if(current.getTime()>expiry.getTime()){
$('.panchang_one').hide();
$('.panchang_two').show();
}
if(current.getTime()>expiry2.getTime()){
$('.panchang_two').hide();
$('.panchang_three').show();
}
if(current.getTime()>expiry3.getTime()){
$('.panchang_three').hide();
$('.panchang_four').show();
}
if(current.getTime()>expiry4.getTime()){
$('.panchang_four').hide();
$('.panchang_five').show();
}
if(current.getTime()>expiry5.getTime()){
$('.panchang_five').hide();
$('.panchang_six').show();
}
if(current.getTime()>expiry6.getTime()){
$('.panchang_six').hide();
$('.panchang_seven').show();
}
if(current.getTime()>expiry7.getTime()){
$('.panchang_seven').hide();
$('.panchang_eight').show();
}
if(current.getTime()>expiry8.getTime()){
$('.panchang_eight').hide();
$('.panchang_nine').show();
}
if(current.getTime()>expiry9.getTime()){
$('.panchang_nine').hide();
$('.panchang_ten').show();
}
if(current.getTime()>expiry10.getTime()){
$('.panchang_ten').hide();
$('.panchang_eleven').show();
}
if(current.getTime()>expiry11.getTime()){
$('.panchang_eleven').hide();
$('.panchang_twelve').show();
}
if(current.getTime()>expiry12.getTime()){
$('.panchang_twelve').hide();
$('.panchang_thirteen').show();
}
if(current.getTime()>expiry13.getTime()){
$('.panchang_thirteen').hide();
$('.panchang_fourteen').show();
}
if(current.getTime()>expiry14.getTime()){
$('.panchang_fourteen').hide();
$('.panchang_fifteen').show();
}
if(current.getTime()>expiry15.getTime()){
$('.panchang_fifteen').hide();
$('.panchang_sixteen').show();
}
if(current.getTime()>expiry16.getTime()){
$('.panchang_sixteen').hide();
$('.panchang_seventeen').show();
}
if(current.getTime()>expiry17.getTime()){
$('.panchang_seventeen').hide();
$('.panchang_eighteen').show();
}
if(current.getTime()>expiry18.getTime()){
$('.panchang_eighteen').hide();
$('.panchang_nineteen').show();
}
if(current.getTime()>expiry19.getTime()){
$('.panchang_nineteen').hide();
$('.panchang_twenty').show();
}
if(current.getTime()>expiry20.getTime()){
$('.panchang_twenty').hide();
$('.panchang_twentyone').show();
}
if(current.getTime()>expiry21.getTime()){
$('.panchang_twentyone').hide();
$('.panchang_twentytwo').show();
}
if(current.getTime()>expiry22.getTime()){
$('.panchang_twentytwo').hide();
$('.panchang_twentythree').show();
}
if(current.getTime()>expiry23.getTime()){
$('.panchang_twentythree').hide();
$('.panchang_twentyfour').show();
}
if(current.getTime()>expiry24.getTime()){
$('.panchang_twentyfour').hide();
$('.panchang_twentyfive').show();
}
if(current.getTime()>expiry25.getTime()){
$('.panchang_twentyfive').hide();
$('.panchang_twentysix').show();
}
if(current.getTime()>expiry26.getTime()){
$('.panchang_twentysix').hide();
$('.panchang_twentyseven').show();
}
if(current.getTime()>expiry27.getTime()){
$('.panchang_twentyseven').hide();
$('.panchang_twentyeight').show();
}
if(current.getTime()>expiry28.getTime()){
$('.panchang_twentyeight').hide();
$('.panchang_twentynine').show();
}
if(current.getTime()>expiry29.getTime()){
$('.panchang_twentynine').hide();
$('.panchang_thirty').show();
}
}, 3000);
$('.panchang_one').show();
</script>
希望这有帮助!
答案 1 :(得分:0)
在你的问题中,我发现很奇怪你所有的日子都很难编码。如果您打算使用var current = new Date();
,那么这将获得今天的当前日期,因此您必须每天更新这些硬编码值。
我冒昧地将您的代码转换为更简约的代码。点击Click Me
按钮可模拟相对于今天的当前日期连续30天内的天数。
// Create an ease of use function to add days to a given date
Date.prototype.addDays = function(days) {
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
}
// Start with all divs hidden
$('.panchang').hide();
// This will be called when our button is clicked
function start() {
var today = new Date(); // get today's date
var expiryDates = new Array();
for (var i = 0; i < 30; ++i) // store the dates of the next 30 days
expiryDates.push(new Date().addDays(i));
// Every 100 milli seconds, simulate the passing of 1 day
var interval = setInterval(() => {
var keyDateIndex = null;
// iterate expiry dates and see which dates we have surpassed
for (let [index, value] of expiryDates.entries()) {
if (today.getTime() > value.getTime())
keyDateIndex = index;
else
break;
}
if (keyDateIndex == expiryDates.length - 1)
clearInterval(interval);
else
today = today.addDays(1);
// Show the correct divs
$('.panchang').hide();
$('.panchang:eq(' + keyDateIndex + ')').show();
}, 100);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="start()">Click Me</button>
<div class="panchang">1</div>
<div class="panchang">2</div>
<div class="panchang">3</div>
<div class="panchang">4</div>
<div class="panchang">5</div>
<div class="panchang">6</div>
<div class="panchang">7</div>
<div class="panchang">8</div>
<div class="panchang">9</div>
<div class="panchang">10</div>
<div class="panchang">11</div>
<div class="panchang">12</div>
<div class="panchang">13</div>
<div class="panchang">14</div>
<div class="panchang">15</div>
<div class="panchang">16</div>
<div class="panchang">17</div>
<div class="panchang">18</div>
<div class="panchang">19</div>
<div class="panchang">20</div>
<div class="panchang">21</div>
<div class="panchang">22</div>
<div class="panchang">23</div>
<div class="panchang">24</div>
<div class="panchang">25</div>
<div class="panchang">26</div>
<div class="panchang">27</div>
<div class="panchang">28</div>
<div class="panchang">29</div>
<div class="panchang">30</div>
&#13;