在特定选择器中附加div并获取第一个选择器 [英] Append div in specific selector and get the first one
问题描述
我有一个日历,在该日历中,我选择了一个与array
值匹配的日期,然后将其与append
和div
匹配,并带有一些样式.但是现在的问题是,当day = 1时,它也会在下个月选择第1天的表单,因为这也在我的active slider
I have a calendar where I select a date matches an array
value and than append
a div
to it with some styling. But the problem now is that when the day = 1 it will also select the day 1 form next month because this is also in my active slider
这是我的意思的图片:
Here is a picture of what I mean:
所以基本上我想做的是尝试选择activer-slider
中的第一个,这是我将样式附加到特定日期的代码:
So basically what I want to do is try to select the first one in my activer-slider
here is the code how I append the styling to that specific day:
$('.swiper-slide-active').find('td').each(function(){
if ($(this).text().trim() == splitDate[0].toString()) {
$(this).first().empty().append('<div class="ex-done ex-base">' + splitDate[0] + '</div>');
}
})
因此,正如您所看到的,我尝试使用first()
方法选择第一个方法,但这不会有任何改变.
So as you see is what I tried to select the first one with the first()
method but this will change nothing.
推荐答案
不要在所有td
元素上运行代码,而是先对其进行过滤.
Instead of running your code on all td
elements, filter them first.
$('.swiper-slide-active td:not(.text-muted)')
.filter(function() {
return $(this).text().trim() == splitDate[0].toString();
})
.empty()
.append('<div class="ex-done ex-base">' + splitDate[0] + '</div>');
但是请记住,选择30号时您会遇到相反的问题.它将突出显示上个月的30号.
but keep in mind that you will get the opposite problem when you select the 30th. It will highlight the 30th of the previous month.
因此,注意到前一个/下一个日期具有不同的颜色,我认为它们具有不同的类别,您可能应该使用该类别来标识要过滤的当前月日期.
So, noticing that the previous/next dates are of different color, i would assume they have a different class, and you should probably use that to identify the current-month dates to filter.
这篇关于在特定选择器中附加div并获取第一个选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!