Is there a CSS selector for elements containing certain text?

后端 未结 18 2359
别跟我提以往
别跟我提以往 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:04

    The syntax of this question looks like Robot Framework syntax. In this case, although there is no css selector that you can use for contains, there is a SeleniumLibrary keyword that you can use instead. The Wait Until Element Contains.

    Example:

    Wait Until Element Contains  | ${element} | ${contains}
    Wait Until Element Contains  |  td | male
    
    0 讨论(0)
  • 2020-11-21 05:07

    Looks like they were thinking about it for the CSS3 spec but it didn't make the cut.

    :contains() CSS3 selector http://www.w3.org/TR/css3-selectors/#content-selectors

    0 讨论(0)
  • 2020-11-21 05:07

    Using jQuery:

    $('td:contains("male")')
    
    0 讨论(0)
  • 2020-11-21 05:08

    I agree the data attribute (voyager's answer) is how it should be handled, BUT, CSS rules like:

    td.male { color: blue; }
    td.female { color: pink; }
    

    can often be much easier to set up, especially with client-side libs like angularjs which could be as simple as:

    <td class="{{person.gender}}">
    

    Just make sure that the content is only one word! Or you could even map to different CSS class names with:

    <td ng-class="{'masculine': person.isMale(), 'feminine': person.isFemale()}">
    

    For completeness, here's the data attribute approach:

    <td data-gender="{{person.gender}}">
    
    0 讨论(0)
  • 2020-11-21 05:09

    As CSS lacks this feature you will have to use JavaScript to style cells by content. For example with XPath's contains:

    var elms = document.evaluate( "//td[contains(., 'male')]", node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null )
    

    Then use the result like so:

    for ( var i=0 ; i < elms.snapshotLength; i++ ){
       elms.snapshotItem(i).style.background = "pink";
    }
    

    https://jsfiddle.net/gaby_de_wilde/o7bka7Ls/9/

    0 讨论(0)
  • 2020-11-21 05:09

    @voyager's answer about using data-* attribute (e.g. data-gender="female|male" is the most effective and standards compliant approach as of 2017:

    [data-gender='male'] {background-color: #000; color: #ccc;}
    

    Pretty much most goals can be attained as there are some albeit limited selectors oriented around text. The ::first-letter is a pseudo-element that can apply limited styling to the first letter of an element. There is also a ::first-line pseudo-element besides obviously selecting the first line of an element (such as a paragraph) also implies that it is obvious that CSS could be used to extend this existing capability to style specific aspects of a textNode.

    Until such advocacy succeeds and is implemented the next best thing I could suggest when applicable is to explode/split words using a space deliminator, output each individual word inside of a span element and then if the word/styling goal is predictable use in combination with :nth selectors:

    $p = explode(' ',$words);
    foreach ($p as $key1 => $value1)
    {
     echo '<span>'.$value1.'</span>;
    }
    

    Else if not predictable to, again, use voyager's answer about using data-* attribute. An example using PHP:

    $p = explode(' ',$words);
    foreach ($p as $key1 => $value1)
    {
     echo '<span data-word="'.$value1.'">'.$value1.'</span>;
    }
    
    0 讨论(0)
提交回复
热议问题