Is there a CSS selector for elements containing certain text?

后端 未结 18 2416
别跟我提以往
别跟我提以往 2020-11-21 04:26

I am looking for a CSS selector for the following table:

Peter    | male    | 34
Susanne  | female  | 12

Is there any selector to match all

18条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-21 05:02

    You could set content as data attribute and then use attribute selectors, as shown here:

    /* Select every cell containing word "male" */
    td[data-content="male"] {
      color: red;
    }
    
    /* Select every cell starting on "p" case insensitive */
    td[data-content^="p" i] {
      color: blue;
    }
    
    /* Select every cell containing "4" */
    td[data-content*="4"] {
      color: green;
    }
    Peter male 34
    Susanne female 14

    You can also use jQuery to easily set the data-content attributes:

    $(function(){
      $("td").each(function(){
        var $this = $(this);
        $this.attr("data-content", $this.text());
      });
    });
    

提交回复
热议问题