最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use 'contains' in an if statement? - Stack Overflow

programmeradmin4浏览0评论

I have HTML that looks like this:

<div class="item-list">
 <h3>Monday Sep 21</h3>
 <h3>Tuesday Sep 22</h3>
 <h3>Wednesday Sep 23</h3>

If today's date is on the list, then that date should be red. If today is not on the list (hey, it's still August!), then the 21st should be red. I used this code to successfully turn Sept 21 red, but I don't know how to put it in an if/else. [I tried some basic stuff, and searched, but I am lame with js.]

$(".item-list h3:contains('Monday Sept 21')").css('color','red');

(That "Monday Sept 21" will eventually be a variable based on today's date.)

I have HTML that looks like this:

<div class="item-list">
 <h3>Monday Sep 21</h3>
 <h3>Tuesday Sep 22</h3>
 <h3>Wednesday Sep 23</h3>

If today's date is on the list, then that date should be red. If today is not on the list (hey, it's still August!), then the 21st should be red. I used this code to successfully turn Sept 21 red, but I don't know how to put it in an if/else. [I tried some basic stuff, and searched, but I am lame with js.]

$(".item-list h3:contains('Monday Sept 21')").css('color','red');

(That "Monday Sept 21" will eventually be a variable based on today's date.)

Share Improve this question edited Jul 13, 2011 at 19:14 Lightness Races in Orbit 385k77 gold badges664 silver badges1.1k bronze badges asked Aug 11, 2009 at 22:30 EileenEileen 6,6606 gold badges29 silver badges29 bronze badges 1
  • 2 An example of a good question with a good answer. – Lightness Races in Orbit Commented Jul 13, 2011 at 19:15
Add a comment  | 

2 Answers 2

Reset to default 18

Using the is traversal method:

$(".item-list h3").each(function () {
    if ($(this).is(':contains("Monday Sept 21")')) {
        $(this).css("color", "red");
    } else {
        // do something
    }
});

Thanks to @karim79's answer, I see I can do a simple IF call directly like this:

var val = $('#customTextArea').is(':contains("Enter Text Here")');
发布评论

评论列表(0)

  1. 暂无评论