jQuery:包含搜索特定文本



我在jQuery上遇到了一些困难:包含选择器。我正在制作日历,并且有一些代码可以突出日历上的当前日期,该日期存储在称为TDATE的变量中。我正在将日历中的日期打印在< td&gt's中,上面有一个" day"类,并且我在一个名为Monthbg的Varibale中都有一个固定的背景色。这是我的代码中的问题:

$('.day:contains("' + tdate + '")').css({'background-color': monthBG, 'color': 'white'});

问题是,使用单位日期,它将突出显示包括该数字的日期,但不是当前日期(例如,6、16和26都有数字6他们,因此,如果这是任何给定月的第六个,则列出的所有日期都会以我的当前代码突出显示)。

那么,我将如何选择包含文本值完全等于tdate变量的值的内容?

对于完全相等的文本/值搜索,您可以使用类似的内容:

$('.day').filter(function() {
    return $(this).text()===tdate;
  }).css({'background-color': monthBG, 'color': 'white'});

演示:

tdate='1111';
monthBG='red';
$('.day').filter(function() {
    return $(this).text().trim()===tdate;
  }).css({'background-color': monthBG, 'color': 'white'});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='day'>
1111
</div>
<div class='day'>
2222
</div>

最新更新