我在使用 jQuery :contains 选择器时遇到了一些困难。我正在制作一个日历,并且有一些代码可以突出显示日历上的当前日期,该日期存储在名为 tdate 的变量中。我在
$('.day:contains("' + tdate + '")').css({'background-color': monthBG, 'color': 'white'});
问题在于,对于单位数日期,它将突出显示日期中包含该数字的日期,但不是当前日期(例如,6、16 和 26 中都有数字 6)他们,所以如果它是任何给定月份的 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>
关于javascript - jQuery :contains search for specific text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47614674/