Is there a CSS selector for elements containing certain text?

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

    If you don't create the DOM yourself (e.g. in a userscript) you can do the following with pure JS:

    for ( td of document.querySelectorAll('td') ) {
      console.debug("text:", td, td.innerText)
      td.setAttribute('text', td.innerText)
    }
    for ( td of document.querySelectorAll('td[text="male"]') )
      console.debug("male:", td, td.innerText)
    <table>
      <tr>
        <td>Peter</td>
        <td>male</td>
        <td>34</td>
      </tr>
      <tr>
        <td>Susanne</td>
        <td>female</td>
        <td>12</td>
      </tr>
    </table>

    Console output

    text: <td> Peter
    text: <td> male
    text: <td> 34
    text: <td> Susanne
    text: <td> female
    text: <td> 12
    male: <td text="male"> male
    
    0 讨论(0)
  • 2020-11-21 05:13

    I'm afraid this is not possible, because the content is no attribute nor is it accessible via a pseudo class. The full list of CSS3 selectors can be found in the CSS3 specification.

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

    Doing small Filter Widgets like this:

        var searchField = document.querySelector('HOWEVER_YOU_MAY_FIND_IT')
        var faqEntries = document.querySelectorAll('WRAPPING_ELEMENT .entry')
    
        searchField.addEventListener('keyup', function (evt) {
            var testValue = evt.target.value.toLocaleLowerCase();
            var regExp = RegExp(testValue);
    
            faqEntries.forEach(function (entry) {
                var text = entry.textContent.toLocaleLowerCase();
    
                entry.classList.remove('show', 'hide');
    
                if (regExp.test(text)) {
                    entry.classList.add('show')
                } else {
                    entry.classList.add('hide')
                }
            })
        })
    
    0 讨论(0)
  • 2020-11-21 05:14

    You could also use content with attr() and style table cells that are :not :empty:

    th::after { content: attr(data-value) }
    td::after { content: attr(data-value) }
    td[data-value]:not(:empty) {
      color: fuchsia;
    }
    <table>
      <tr>
        <th data-value="Peter"></th>
        <td data-value="male">&#x0200B;</td>
        <td data-value="34"></td>
      </tr>
      <tr>
        <th data-value="Susanne"></th>
        <td data-value="female"></td>
        <td data-value="12"></td>
      </tr>
      <tr>
        <th data-value="Lucas"></th>
        <td data-value="male">&#x0200B;</td>
        <td data-value="41"></td>
      </tr>
    </table>

    A ZeroWidthSpace is used to change the color and may be added using vanilla JavaScript:

    const hombres = document.querySelectorAll('td[data-value="male"]');
    hombres.forEach(hombre => hombre.innerHTML = '&#x0200B;');
    

    Although the <td>s end tag may be omitted doing so may cause it to be treated as non-empty.

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

    You'd have to add a data attribute to the rows called data-gender with a male or female value and use the attribute selector:

    HTML:

    <td data-gender="male">...</td>
    

    CSS:

    td[data-gender="male"] { ... }
    
    0 讨论(0)
  • 2020-11-21 05:16

    I find the attribute option to be your best bet if you don't want to use javascript or jquery.

    E.g to style all table cells with the word ready, In HTML do this:

     <td status*="ready">Ready</td>
    

    Then in css:

    td[status*="ready"] {
            color: red;
        }
    
    0 讨论(0)
提交回复
热议问题