简体   繁体   English

根据日期更改链接

[英]Change link depending on date

I have been strugling with this all evening. 我整晚都在努力。

http://codepen.io/Casfan/pen/xOPKYE?editors=1111 http://codepen.io/Casfan/pen/xOPKYE?editors=1111

I have a list of items 我有项目清单

  <ul> <li><span class="glyphicon glyphicon-flag" aria-hidden="true"></span> Blackwell Grange <time datetime="2016-07-23">23/07/2016</time> 12:30 *Prudhoe, Garesfield <a href="#" class ="datelink">THIS LINK SHOULD SHOW NOW</a> 


   <li><span class="glyphicon glyphicon-flag" aria-hidden="true"></span> Blackwell Grange <time datetime="2016-07-23">23/07/2016</time> 12:30 *Prudhoe, Garesfield <a href="#" class ="datelink">THIS LINK SHOULD SHOW NOW</a> 
<a class="resultdate" id="tempresults" href='https://www.dropbox.com/sh/4gxqq4pej9c58oa/AAChi69GfE1Mf58auVeV5jf9a?dl=0'>THIS LINK SHOULD SHOW IF THE DATE IN DATE TIME IS SET TO THE PAST</a></li>
... etc
</ul>

css 的CSS

.datelink {
    display:none;
}

.resultdate {
  display:none;
}

So the links are hidden to start with and then using coffeescript because Im doing this in RoR I have 因此链接被隐藏起来,然后再使用coffeescript,因为我在RoR中这样做

$(document).ready ->
now = undefined
now = new Date

$('ul li time').each ->
    dateTime = Date.parse($(this).attr('datetime'))
    startdate = new Date(dateTime)
    startdate.setDate startdate.getDate() - 14
    enddate = new Date(dateTime)
    enddate.setDate enddate.getDate() - 2
    if now > startdate and now < enddate
        $(this).next('a.datelink').show()
return

$('ul li time').each ->
  dateTime = Date.parse($(this).attr('datetime'))
  fixturedate = new Date(dateTime)
  if now > fixturedate
   $( "#tempresults" ).show()
return  
return

I am using a time datetime atribute in the html to set the date for the fixture The first function in there works and shows the link when we are between the two dates. 我在html中使用时间datetime属性来设置灯具的日期。其中的第一个功能有效,并显示了我们在两个日期之间的链接。 The second function I want to display the other link with either the class of resultdate or id of tempresults if the date is in the past only one of the list items have this class or id. 第二个功能是,如果日期是过去的日期,则仅显示列表项中的一个具有此类或id,因此我想显示带有resultdate或tempresults id的其他链接。

I think the logic works when I only have one of the functions, when I have them both the bottom one does not work. 我认为当我只有一个功能时,逻辑就起作用了;当我同时拥有这两个功能时,底层的逻辑就不起作用了。 I have tried combinding it into one function but then only the top if statement works. 我已经尝试将其组合为一个函数,但是只有if语句可以运行。 I have tried using $(this).next('a.resultdate').show() 我尝试使用$(this).next('a.resultdate')。show()

What am I doing wrong? 我究竟做错了什么?

Your last call is $( "#tempresults" ).show() . 您的最后一个通话是$( "#tempresults" ).show() How many links have the id #tempresults ? ID #tempresults有多少个链接?

If you have more than one of them, perhaps a $(this).child(".resultdate").show() would target the right ones. 如果您有多个,则$(this).child(".resultdate").show()会定位正确的对象。

You don't need the return s. 您不需要return It is exiting after the first loop. 它在第一个循环之后退出。 see my forked pen 看到我的叉子

$(document).ready ->
  now = undefined
  now = new Date

  $('ul li time').each ->
    dateTime = Date.parse($(this).attr('datetime'))
    startdate = new Date(dateTime)
    startdate.setDate startdate.getDate() - 14
    enddate = new Date(dateTime)
    enddate.setDate enddate.getDate() - 2
    if now > startdate and now < enddate
      $(this).next('a.datelink').show()

  $('ul li time').each ->
    dateTime = Date.parse($(this).attr('datetime'))
    fixturedate = new Date(dateTime)
    if now > fixturedate
      $( "#tempresults" ).show()

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

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