如何通过jQuery获得选定的时间并添加类? [英] How can I get selected time and add class via jQuery?
本文介绍了如何通过jQuery获得选定的时间并添加类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的HTML是动态的,如下所示:
My HTML is dynamic and like that:
<div class="date">
<strong>21</strong>
<span class="month">Jan</span>
<strong class="year">15</strong>
<span class="details"></span>
</div>
我想得到这个时间,并通过jQuery打印控制台:
And I want get this time and print console via jQuery:
var selectedDate = $('.date');
console.log(selectedDate);
但结果不正确。如何修复?
But result is not correct. How can I fix it?
编辑:
好的,我解决了我的问题:
Ok, I solve my problem with that:
console.log(selectedDate.text());
现在,我想添加类if event是一个过去的事件。
Now, I want add class if event is a past event.
if (selectedDate < now) {
$('.event').addClass('past');
}
但没有任何错误或结果。
But not any error or result.
推荐答案
尝试这样:
selectedDate = $(".date > .month").text() + " "
+ $(".date > strong:nth-child(1)").text() +
" 20"+ $(".date > .year").text();
console.log(selectedDate); // Jan 21 2015
当然,您可以重新安排这些按照您想要的格式 -
You could, of course, re-arrange those to output the date according to the format you want -
这篇关于如何通过jQuery获得选定的时间并添加类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文