[英]How do I show elements that have start date in one month and end date in another?
I am trying to only show elements that are related to certain months. 我试图仅显示与某些月份相关的元素。 I am using PHP to make a select box that value's are the months' first and last days of the month. 我正在使用PHP创建一个选择框,其值是月份的第一天和最后一天。
Here is the resulting HTML for the select box: 这是选择框的结果HTML:
<select id="month" name="monthdaterange">
<option value="1427864400,1430370000">April</option>
<option value="1430456400,1433048400">May</option>
<option value="1433134800,1435640400">June</option>
<option value="1435726800,1438318800">July</option>
<option value="1438405200,1440997200">August</option>
<option value="1441083600,1443589200">September</option>
<option value="1443675600,1446267600">October</option>
<option value="1446354000,1448863200">November</option>
<option value="1448949600,1451541600">December</option>
<option value="1451628000,1454220000">January</option>
<option value="1454306400,1456725600">February</option>
<option value="1456812000,1459400400">March</option>
</select>
This is one related element: 这是一个相关的元素:
<div data-start-date="1429765200" data-end-date="1431061200" class="small-12 medium-6 large-6 columns special-container">
<div class="special-block">
<div class="special-block-text">
<h3>Special Test 1</h3>
<p>Lorem ipsum dolor sit amet....</p>
</div>
</div>
</div>
As you can see I am using data-start-date
and data-end-date
to store the start and end date of the element. 如您所见,我正在使用data-start-date
和data-end-date
来存储元素的开始和结束日期。
Then I am using jQuery to compare the element date range to the select box values. 然后,我使用jQuery将元素日期范围与选择框值进行比较。
function get_month_specials(mnth){
var daterange = mnth.split(',');
var firstofmonth = daterange[0];
var lastofmonth = daterange[1];
//alert('The start date of this month is: ' + firstofmonth + '. The end date of this month is: ' + lastofmonth);
jQuery('.special-container').each( function( index, value ){
if(jQuery(this).attr('data-start-date') >= firstofmonth || jQuery(this).attr('data-end-date') <= lastofmonth){
jQuery(this).css( 'background-color', '#fff' );
}
});
}
The issue I am having is with the above element. 我遇到的问题是上述元素。 It has a start date in April and an end date in May. 它的开始日期为4月,结束日期为5月。 This means it should be displayed in both months when selected. 这意味着选择后应在两个月中都显示它。 But instead it only displays in April. 但相反,它仅在4月显示。 What am I missing to make it so elements that have a date range between two or more months display when any of those months are selected? 我缺少什么使之成为日期范围在两个或多个月之间的元素,而在选择这些月份中的任何一个时会显示出来呢?
Here is a Codepen: http://codepen.io/anon/pen/wBLwwv 这是一个Codepen: http ://codepen.io/anon/pen/wBLwwv
If you're only bothered about if it is in that month then just use the start dates of the month. 如果您只是在那个月才被打扰,那么只需使用该月的开始日期即可。
Change this line 更改此行
if(jQuery(this).attr('data-start-date') >= firstofmonth || jQuery(this).attr('data-end-date') >= lastofmonth){
To 至
if(jQuery(this).attr('data-start-date') >= firstofmonth || jQuery(this).attr('data-end-date') >= firstofmonth ){
It doesn't matter when the month ends you just need to know if the event is ongoing past the 1st of the month 每月的结束时间无关紧要,您只需要知道活动是否在每月的1号之后进行即可
the logic is wrong, try this 逻辑错误,请尝试
$('.special-container').each( function( index, value ){
$(this).addClass('hidden');
if(
($(this).attr('data-start-date') >= firstofmonth && $(this).attr('data-start-date') <= lastofmonth) ||
($(this).attr('data-end-date') <= lastofmonth && $(this).attr('data-end-date') >= firstofmonth)) {
$(this).removeClass('hidden');
}
});
Edited: final fix: "looks" both way 编辑:最终修正:双向“看起来”
$('.special-container').each( function( index, value ){
$(this).addClass('hidden');
if(
(firstofmonth >= $(this).attr('data-start-date') && firstofmonth <= $(this).attr('data-end-date')) ||
(lastofmonth >= $(this).attr('data-start-date') && lastofmonth <= $(this).attr('data-end-date')) ||
($(this).attr('data-start-date') >= firstofmonth && $(this).attr('data-start-date') <= lastofmonth)
) {
$(this).removeClass('hidden');
}
});
http://codepen.io/anon/pen/qEzZvw http://codepen.io/anon/pen/qEzZvw
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.