搜尋

首頁  >  問答  >  主體

CSS nth-chlld 只適用於可見元素?

我知道 :nth-child 實際上是在檢查“子項”與“可見子項”,但是是否有一個選擇器可以處理可見子項?

假設我有一個表格,我將奇數行設定為不同的顏色

我有一個搜尋過濾器,可以隱藏與搜尋不匹配的行。現在,當我搜尋時,行不再交替顏色。

當然,我可以向每個元素添加/刪除類,我基本上已經這樣做來隱藏/顯示它們,但我想我會問是否有 CSS 方法可以做到這一點。

const searchElem = document.querySelector('input');
const tableElem = document.querySelector('table');

function search() {
  const str = searchElem.value.toLowerCase();
  const rows = tableElem.querySelectorAll('tr');

  rows.forEach(function(row){
    const text = row.textContent.toLowerCase();
    if (str.length && !text.includes(str)) {
      row.classList.add('hide');
    } else {
      row.classList.remove('hide');
    }
  });
}

searchElem.addEventListener('keyup', search);
tr {
  background-color: #CDF;
}

tbody>tr:nth-child(odd) {
  background-color: #DEF;
}

thead>tr {
  background-color: lightgreen;
}

.hide {
  display: none;
}
<input type="search" placeholder="search">
<table>
  <thead>
    <tr><td>Name</td><td>Amount</td></tr>
  </thead>
  <tbody>
    <tr><td>Apple</td><td>220</td></tr>
    <tr><td>Watermelon</td><td>465</td></tr>
    <tr><td>Orange</td><td>94</td></tr>
    <tr><td>Pear</td><td>567</td></tr>
    <tr><td>Cherry</td><td>483</td></tr>
    <tr><td>Strawberry</td><td>246</td></tr>
    <tr><td>Nectarine</td><td>558</td></tr>
    <tr><td>Grape</td><td>535</td></tr>
    <tr><td>Mango</td><td>450</td></tr>
    <tr><td>Blueberry</td><td>911</td></tr>
    <tr><td>Pomegranate</td><td>386</td></tr>
    <tr><td>Carambola</td><td>351</td></tr>
    <tr><td>Plum</td><td>607</td></tr>
    <tr><td>Banana</td><td>292</td></tr>
    <tr><td>Raspberry</td><td>912</td></tr>
    <tr><td>Mandarin</td><td>456</td></tr>
    <tr><td>Jackfruit</td><td>976</td></tr>
    <tr><td>Papaya</td><td>200</td></tr>
    <tr><td>Kiwi</td><td>217</td></tr>
    <tr><td>Pineapple</td><td>710</td></tr>
    <tr><td>Lime</td><td>983</td></tr>
    <tr><td>Lemon</td><td>960</td></tr>
    <tr><td>Apricot</td><td>647</td></tr>
    <tr><td>Grapefruit</td><td>861</td></tr>
    <tr><td>Melon</td><td>226</td></tr>
    <tr><td>Coconut</td><td>868</td></tr>
    <tr><td>Avocado</td><td>385</td></tr>
    <tr><td>Peach</td><td>419</td></tr>
  </tbody>
</table>

P粉077701708P粉077701708301 天前373

全部回覆(1)我來回復

  • P粉245489391

    P粉2454893912024-01-30 09:27:08

    沒有選擇器,但如果您願意接受這種情況的特定解決方案,那麼您可以依賴如下所示的漸變:

    const searchElem = document.querySelector('input');
    const tableElem = document.querySelector('table');
    
    function search() {
      const str = searchElem.value.toLowerCase();
      const rows = tableElem.querySelectorAll('tr');
    
      rows.forEach(function(row){
        const text = row.textContent.toLowerCase();
        if (str.length && !text.includes(str)) {
          row.classList.add('hide');
        } else {
          row.classList.remove('hide');
        }
      });
    }
    
    searchElem.addEventListener('keyup', search);
    thead>tr {
      background-color: lightgreen;
    }
    
    .hide {
      display: none;
    }
    
    table {
      position:relative; /* relative to all the table */
      z-index: 0;
    }
    td {
      line-height: 1.2em; /* the height */
      clip-path: inset(0); /* clip the pseudo element to td */
    }
    tbody td:before {
      content: "";
      position: absolute;
      z-index: -1;
      inset: 0;
      background:
        repeating-linear-gradient(
          #CDF  0 calc(1.2em + 4px), /* height   + 2*border-spacing */
          #DEF  0 calc(2.4em + 8px)  /* 2*height + 4*border-spacing */
        ); 
    }
    
    
    NameAmount
    Apple220
    Watermelon465
    Orange94
    Pear567
    Cherry483
    Strawberry246
    Nectarine558
    Grape535
    Mango450
    Blueberry911
    Pomegranate386
    Carambola351
    Plum607
    Banana292
    Raspberry912
    Mandarin456
    Jackfruit976
    Papaya200
    Kiwi217
    Pineapple710
    Lime983
    Lemon960
    Apricot647
    Grapefruit861
    Melon226
    Coconut868
    Avocado385
    Peach419

    回覆
    0
  • 取消回覆