简体   繁体   English

jQuery如果发布日期早于今天,则将其移至另一个div

[英]jQuery if post date is older than today move it to another div

I have an events page with two divs, one that holds upcoming events and another that holds past events. 我有一个包含两个div的事件页面,一个页面包含即将发生的事件,另一个页面则包含过去的事件。

I would like a script that automatically checks the date of each event in the upcoming events div and if any event's date is in the past (before TODAY) then it should automatically move that event to the past events div. 我想要一个脚本,该脚本会自动检查即将发生的事件div中每个事件的日期,如果有任何事件的日期是过去的日期(今天之前),那么它将自动将该事件移至过去的事件div。

My markup is something like: 我的标记是这样的:

<div class="all-events">

    <div class="upcoming-events">
        <div class="event">
            <p class="event-date"></p>
            .
            .
            .

        </div>
    </div>
    <div class="past-events">
        <div class="event">
            <p class="event-date"></p>
            .
            .
            .

        </div>
    </div>
</div>  

I want the script to 我想要脚本

After some research I found this code to compare two things using javascript: 经过研究后,我发现此代码可以使用javascript比较两件事:

<script>
 var date = document.getElementById("inputDate").value;
var varDate = new Date(date); //dd-mm-YYYY
var today = new Date();
today.setHours(0,0,0,0);

if(varDate >= today) {
    alert("Working!");  
}

</script>

But I am not sure if this might help in my case. 但是我不确定这是否对我的情况有所帮助。

Thanks 谢谢

You should not move events from upcoming to past, instead you should add them directly to the past div if they were created in the past. 您不应该将事件从即将发生的事件移到过去,而应该将事件直接创建到过去的div中(如果它们是过去创建的)。

This should do it: 应该这样做:

var postDateInput = $('.event-date').text();
var postDate = new Date(postDateInput);
var todayDate = new Date();
todayDate.setHours(0,0,0,0);

if(postDate < todayDate) {
  // past events
}

I'm a beginner but I want to try: 我是初学者,但我想尝试:

var eventdates = document.querySelectorAll(".event-date");
var today = new Date();
today.setHours(0,0,0,0);

// Loop of all event dates
for (var i = 0; i < eventdates.length; i++) {
  var varDate = new Date(eventdates[i].textContent); 

  // Compare date
  if(varDate < today) {

    //Get Parent node of each <p class="event-date">...</p>
    var event = eventdates[i].parentNode;

    // Get the node index of <div class="events" when moving to upcoming events
    var event_index = [].indexOf.call(event.parentNode.children, event);

    // Get upcoming events container                                  
    var upcoming_events = document.querySelector(".upcoming-events");

    // Get past events container
    var past_events = document.querySelector(".past-events");

    // move the <div class="events" with node index to upcoming events 
    upcoming_events.appendChild(past_events.childNodes[event_index]);
  }
}

Tell me if this works. 告诉我这是否有效。 Thank you. 谢谢。

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

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