admin管理员组

文章数量:1435859

I have the following structure of row in my table:

<tr class="mattersRow">
    <td></td>
    <td colspan="16">
        <div class="dropdown">
            <a href="#" data-toggle="dropdown">Choose QB</a>
            <ul class="dropdown-menu" aria-labelledby="dLabel" role="menu"> … </ul>
        </div>
    </td>
</tr>

Note: I use bootstrap library

I need to set to every <td> that has text inside "" (empty), or "Choose QB" class='danger'.

This is my jQuery code:

$("#tblMatters .mattersRow td").each(function () {
      if ($(this).html() == "" || $(this).innerHTML == "Choose QB")
      {
           $(this).addClass("danger");
           flag = true;
      }
});

Of course, $(this).html() == "" works great, but how to set "danger" class to the <td> with Choose QB

I have the following structure of row in my table:

<tr class="mattersRow">
    <td></td>
    <td colspan="16">
        <div class="dropdown">
            <a href="#" data-toggle="dropdown">Choose QB</a>
            <ul class="dropdown-menu" aria-labelledby="dLabel" role="menu"> … </ul>
        </div>
    </td>
</tr>

Note: I use bootstrap library

I need to set to every <td> that has text inside "" (empty), or "Choose QB" class='danger'.

This is my jQuery code:

$("#tblMatters .mattersRow td").each(function () {
      if ($(this).html() == "" || $(this).innerHTML == "Choose QB")
      {
           $(this).addClass("danger");
           flag = true;
      }
});

Of course, $(this).html() == "" works great, but how to set "danger" class to the <td> with Choose QB

Share Improve this question asked Nov 15, 2013 at 16:07 BryukBryuk 3,34511 gold badges49 silver badges76 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 3

You may use :contains()doc and :emptydoc selectors :

$('#tblMatters .mattersRow')
    .find('td:contains(\'Choose QB\'),td:empty')
    .addClass('danger');

Here is a demo : http://jsfiddle/wared/a84CW/.

addClass accepts a function as an argument, and within that function you have access to the currently iterated td element, and can check what it contains :

$("#tblMatters .mattersRow td").addClass(function() {
    var txt = $.trim( $('a', this).text() );

    return txt.length === 0 || txt == 'Choose QB' ? 'danger' : '';
});

If you're only trying to check the text inside the anchor, you should specify that in the question ?

Use $(this).text().trim() === 'Choose QB'

Try .closest()

$(this).closest('td').addClass("danger");

Use .filter()

$("#tblMatters .mattersRow td").filter(function () {
    var txt = $.trim($(this).text());
    return txt.length === 0 || txt === 'Choose QB';
}).addClass("danger");

or check text of anchor tag

$("#tblMatters .mattersRow td a").filter(function () {
    var txt = $.trim($(this).text());
    return txt.length === 0 || txt === 'Choose QB';
}).closest('td').addClass("danger");


This should work for both

$("#tblMatters .mattersRow td a").filter(function () {
    var txt = $.trim($(this).text());
    return $(this).closest('td').text().length === 0 || txt === 'Choose QB';
}).closest('td').addClass("danger");

本文标签: jqueryHow to get text of link that inside td in HTML Table using JavaScriptStack Overflow