简体   繁体   English

如果匹配日期则隐藏

[英]Hide if matching dates

I have dates being outputted on a events list and I want to hide any end dates that match the start date. 我在事件列表中输出了日期,并且我想隐藏与开始日期匹配的任何结束日期。

For example; 例如;

<span class="start_date">Wed 23rd January</span>

<span class="end_date">Wed 23rd January</span>

I'm trying to add a class to the span around the end date when the date matches the start date, so I can hide it. 当日期与开始日期匹配时,我正在尝试向结束日期周围的范围添加一个类,因此可以将其隐藏。

Here is the JS I've tried to use so far to see if the two fields match. 到目前为止,这是我尝试使用的JS,以查看两个字段是否匹配。

var a = $('.start_date');
var b = $('.end_date');

if ($.data(a) == $.data(b)) {
    $('.end_date').addClass('hide');
}

Just use text() instead: 只需使用text()代替:

 var a = $('.start_date'), b = $('.end_date'); if( a.text() == b.text() ) { $('.end_date').addClass('hide'); } 
 .hide { display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="start_date">Wed 23rd January</span> <span class="end_date">Wed 23rd January</span> 

Look, no jQuery, :-) 看,没有jQuery,:-)

 let a = document.querySelector('.start_date'); let b = document.querySelector('.end_date'); if (a.textContent == b.textContent) { b.classList.add('hide'); } 
 .hide { display: none; } 
 <span class="start_date">Wed 23rd January</span> <span class="end_date">Wed 23rd January</span> 

But you may want to trim any leading or trailing whitespace or do other processing to ensure things aren't affected by minor formatting differences (eg upper/lower case). 但是您可能想要修剪任何前导或尾随空格,或进行其他处理以确保内容不受较小的格式差异(例如,大写/小写)的影响。

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

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