CSS Selector: Select TR where TD contains special value

五迷三道 提交于 2019-12-08 14:07:00

问题


I have a Table like this:

<table>
    <tr>
        <td>
            row4545
        </td>
    </tr>
    <tr>
        <td>
            row22
        </td>
    </tr>
    <tr>
        <td>
            row6767
        </td>
    </tr>
</table>

I want to set a background-color on the tr which contains the value row22 in a td. Is it possible to select it somehow with CSS?

PS: I use jQuery.


回答1:


Try

$('tr td').filter(function(){
    return $.trim($(this).text()) === "row22";
}).parent().css('background-color', 'blue')

Demo: Fiddle




回答2:


$('td').filter(function(){
     return $.trim($(this).text()) === "row22";
}).closest('tr').css('color','red');

Demo ---> http://jsfiddle.net/jJWs2/2/




回答3:


You can write this:

$(document).ready(function() {
    $( "td:contains(row22)").css("background-color","red");
});



回答4:


You can try this:

$(function(){
    $("td:contains('row22')").filter(function(){        
        $.trim($(this).text()) == 'row22' ? $(this).parent().attr('style','background:red;') : '';
    });
});



回答5:


This should Help..

$('tr td').filter(function() {
    return $(this).text() == "row22";
}).parent().css('background-color','red');



回答6:


<table>
    <tr style="background:#000;">
        <td>
            row4545
        </td>
    </tr>
    <tr>
        <td>
            row22
        </td>
    </tr>
    <tr>
        <td>
            row6767
        </td>
    </tr>
</table>


来源:https://stackoverflow.com/questions/17188896/css-selector-select-tr-where-td-contains-special-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!