可点击的div基于当前日期

时间:2016-03-07 12:15:44

标签: javascript jquery html css3 clickable

我有一个包含多个div的页面,其中包含带链接的图像,我想根据日期将其从无法点击变为可点击。 例如 - 我有5天的5个div(2016年3月1日,2016年3月3日,2016年3月3日,2016年3月3日,2016年3月5日)。在这些div中,我有链接的图像。

于2016年3月1日 - 只有div 1可点击,其他所有不是

于02.03.2016 - 只有div1和div2可点击,其他所有都没有 等等......

于05.03.2016 - 所有五个div都可点击

提前谢谢

3 个答案:

答案 0 :(得分:0)

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

<强> 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

答案 1 :(得分:0)

&#13;
&#13;
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';
  }
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

  

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');
   }
相关问题