简体   繁体   English

如何遍历元素及其子元素以顺序添加或删除类— Javascript / jQuery

[英]How to loop through elements and their sub elements in order add or remove classes — Javascript/jQuery

I have a list of events – I want to only show the ones that are in the past. 我有一个事件列表–我只想显示过去的事件。 To do this I was thinking to loop though these elements, find the containing div with the date, and then compare this to todays current date. 为此,我想遍历这些元素,找到包含日期的div,然后将其与今天的当前日期进行比较。

A basic structure of these events is as follows: 这些事件的基本结构如下:

<div class="past-event">

  <span class="event-title">Event 1</span>
  <span class="event-date">05.05.19</span>

</div>

<div class="past-event">

  <span class="event-title">Event 2</span>
  <span class="event-date">20.04.19</span>

</div>

I've used Moment.js to parse the dates. 我用Moment.js解析日期。 I've used jQuery for this so far (I usually try not to) but this is hosted on a Wordpress site anyway so jQuery is already available. 到目前为止,我已经为此使用了jQuery(我通常不这样做),但是无论如何它都托管在Wordpress网站上,因此jQuery已经可用。 I'm happy to hear any solution and not just a jQuery related one. 我很高兴听到任何解决方案,而不仅仅是与jQuery相关的解决方案。

Here is a JSFiddle of a simplified version of my code. 这是我的代码的简化版本的JSFiddle。 The way its laid out, the first two elements should be hidden and the second two should be blue (I have added a "blue" class to make it clearer when the code is not working) 布局方式,前两个元素应该隐藏,后两个元素应该是蓝色(我添加了一个“蓝色”类,以便在代码无法正常工作时更加清楚)

https://jsfiddle.net/ngpvkscf/ https://jsfiddle.net/ngpvkscf/

There's a couple of issues here: 这里有几个问题:

  • You're trying to create event_date as a moment object, yet you're passing the value of itself instead of event_date_raw 您正在尝试将event_date创建为矩对象,但是正在传递其自身的值而不是event_date_raw
  • The resulting dates will be a moment object and a string. 结果日期将是一个矩对象和一个字符串。 For the comparison to work you need to equalise the types. 为了进行比较,您需要均衡类型。 I'd suggest using plain Date objects, so you need to call .toDate() in moment and use new Date() to get the current date time. 我建议使用普通的Date对象,因此您需要立即调用.toDate()并使用new Date()来获取当前日期时间。
  • You need to use find() to get the .event-date element within the current .past-event . 您需要使用find()来获取当前.past-event.event-date元素。 Your current logic is only working with the first one. 您当前的逻辑仅适用于第一个逻辑。

With those issues fixed, it works: 解决这些问题后,它可以工作:

 $('.past-event').each(function(i, obj) { var event_date_raw = $(this).find('.event-date').text(); var event_date = moment(event_date_raw, "DD.MM.YY").toDate(); var now = new Date(); if (now < event_date) { $(this).addClass("hidden"); } else { $(this).addClass("blue"); } }); 
 body { display: flex; width: 100%; flex-wrap: ; /* fix this too */ margin: 0; color: #EEE; } .past-event { width: 50%; display: flex; flex-direction: column; } .blue { background-color: blue; } .hidden { display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script> <div class="past-event"> <span class="event-title">Event 1</span> <span class="event-date">05.05.19</span> </div> <div class="past-event"> <span class="event-title">Event 2</span> <span class="event-date">20.04.19</span> </div> <div class="past-event"> <span class="event-title">Event 3</span> <span class="event-date">01.02.19</span> </div> <div class="past-event"> <span class="event-title">Event 4</span> <span class="event-date">05.05.18</span> </div> 

I believe the following is what you want: 我相信以下是您想要的:

https://jsfiddle.net/4dehv2pj/ https://jsfiddle.net/4dehv2pj/

jQuery('.past-event').each(function(i, obj) {
  var event_date_raw = jQuery(this).children('.event-date').text();
  var event_date     = moment(event_date_raw, "DD/MM/YY");
  var now              = moment().format("DD/MM/YY");
console.log(event_date_raw);
  if (now < event_date) {
    jQuery(this).addClass("hidden");
  } else {
    jQuery(this).addClass("blue");
  }
});

Comparing dates with moment.js is a little bit different than just a greater than/less than operator. 将数据与moment.js进行比较有点不同,只是大于或小于运算符。

I'm using moment(event_date).isAfter(now) 我正在使用moment(event_date).isAfter(now)

This will get you the correct comparison. 这将为您提供正确的比较。

Also by using jQuery('.event-date').text(); 同样通过使用jQuery('.event-date').text(); you're getting ALL of the elements that match .event-date , so instead I do something like jQuery(this).children('.event-date').text(); 您将获得与.event-date匹配的所有元素,所以我改为执行类似jQuery(this).children('.event-date').text(); , which takes the current element and checks the children of the current element for that query selector. ,它接受当前元素并检查该查询选择器的当前元素的子代。

This is what you may need 这就是你可能需要的

    $('.event-date').each((index, item) => {
    var date = moment($(item).text(), 'DD.MM.YY').toDate();

    if (date > new Date()) {
        console.log(date)
        $(item).closest('.past-event').css('display', 'none');
    }

});

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

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