基于当前日期的可点击 div



我有一个包含多个div的页面,其中包含带有链接的图像,我想根据日期将其从不可点击变为可点击。例如 - 我有 5 天div (01.03.2016, 02.03.2016, 03.03.2016, 04.03.2016, 05.03.2016).在这些div中,我有带有链接的图像。

on 01.03.2016 - 只有div 1 可点击,所有其他

不可点击

on 02.03.2016 - 只有div1 和div2 可点击,所有其他不能等等...

on 05.03.2016 - 所有五个div都可以点击

提前谢谢你

var divs = document.getElementsByClassName("date");
for (i = 0 ; i < divs.length ; i++) {
  divs[i].style.pointerEvents = 'none';
}
for (i = 0 ; i < divs.length ; i++) {  
  var divDate = divs[i].id.split('.');
  var date = new Date(divDate[2], divDate[1] - 1, divDate[0]);
  
  if (date <= new Date()) {
    divs[i].style.pointerEvents = 'auto';
  }
}
<div class="date" id="01.03.2016">01.03.2016 <a href="ihttp://google.com"><img src="images/image1.jpg" alt="Google Image" width="138" height="138" class="img_rounded" /></a></div>
<div class="date" id="02.03.2016">02.03.2016 <a href="ihttp://google.com"><img src="images/image1.jpg" alt="Google Image" width="138" height="138" class="img_rounded" /></a></div>
<div class="date" id="03.03.2016">03.03.2016 <a href="ihttp://google.com"><img src="images/image1.jpg" alt="Google Image" width="138" height="138" class="img_rounded" /></a></div>
<div class="date" id="04.03.2016">04.03.2016 <a href="ihttp://google.com"><img src="images/image1.jpg" alt="Google Image" width="138" height="138" class="img_rounded" /></a></div>
<div class="date" id="09.03.2016">09.03.2016 <a href="ihttp://google.com"><img src="images/image1.jpg" alt="Google Image" width="138" height="138" class="img_rounded" /></a></div>

我会使用日期比较。也许是这样的:

.HTML

<div class="date">01.03.2016</div>
<div class="date">02.03.2016</div>
<div class="date">03.03.2016</div>
<div class="date">04.03.2016</div>
<div class="date">05.03.2016</div>

JavaScript:

var elms = document.getElementsByClassName("date");
for (var i = 0 ; i < elms.length ; i++)
{
   var el = elms[i];
   var dmy = el.innerHTML.split('.');
   var date = new Date(dmy[2], dmy[1] - 1, dmy[0]);
   if (date <= new Date()) {
      el.style.backgroundColor = "yellow";
      el.onclick = function () {
         alert("you clicked on " + this.innerHTML)        
      };
   }
}

演示这是一个JSFiddle

.HTML

 <div class="date" id="First">01.03.2016</div>
 <div class="date all" id="Second">02.03.2016</div>
 <div class="date all three two"   id="Third">03.03.2016</div>
 <div class="date all three two one"   id="Fourth">04.03.2016</div>
 <div class="date all three two one zero"  id="Fifth">05.03.2016</div>

在 JQuery 中禁用单击

  $("#First").click(function(){
     $(".all ").off('click');
   }
  $("#Second").click(function(){
     $(".three ")off('click');
   }
  $("#Third").click(function(){
     $(".two")off('click');
   }
  $("#Fourth").click(function(){
     $(".one").off('click');
   }
  $("#Fifth").click(function(){
     $(".zero")off('click');
   }

或在 JQuery 中禁用div

  $("#First").click(function(){
     $(".date").removeAttr('disabled','disabled');
     $(".all ").attr('disabled','disabled');
   }
  $("#Second").click(function(){
     $(".date").removeAttr('disabled','disabled');
     $(".three ").attr('disabled','disabled');
   }
  $("#Third").click(function(){
     $(".date").removeAttr('disabled','disabled');
     $(".two").attr('disabled','disabled');
   }
  $("#Fourth").click(function(){
     $(".date").removeAttr('disabled','disabled');
     $(".one").attr('disabled','disabled');
   }
  $("#Fifth").click(function(){
     $(".date").removeAttr('disabled','disabled');
     $(".zero").attr('disabled','disabled');
   }

相关内容

  • 没有找到相关文章

最新更新