Come utilizzare querySelectorAll solo per gli elementi che hanno uno specifico elemento del bambino?

0

Domanda

    <table>
      <tr>
        <th>Column name</th>
        <th>Column name</th>
      </tr>
      <tr>
        <td>Column value</td>
        <td>Column value</td>
      <tr>
    </table>
    <table>
      <tr>
        <th>Column name</th>
        <th>Column name</th>
      </tr>
      <tr>
        <td>Column value</td>
        <td>Column value</td>
      <tr>
    </table>

Mi piacerebbe processo tutti tr che contengono un tdma può solo trovare contenere le query per gli attributi e contenuto; non elementi. È l'ultimo possibile, con una sola query?

dom javascript
2021-11-22 10:38:43
1

Migliore risposta

2

Non direttamente, ma è possibile utilizzare filter per ottenere le righe appropriate

const rowsWithTd = [...document.querySelectorAll("table tr")].filter( x => x.querySelector("td"))
console.log(rowsWithTd.map(x => x.outerHTML))
<table>
  <tr>
    <th>Column name</th>
  </tr>
  <tr>
    <td>Column value</td>
  <tr>
</table>
<table>
  <tr>
    <th>Column name</th>
  </tr>
  <tr>
    <td>Column value</td>
  <tr>
</table>

2021-11-22 10:47:03

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................