Select odd even child excluding the hidden child

后端 未结 9 1406
暖寄归人
暖寄归人 2020-12-01 23:30

Line 3 is a hidden

. I don\'t want that one to be taken from the odd/even css rule.

What is the best approach to get t

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 23:49

    Another way, albeit on the fringe side, is to have an extra and either move or copy rows there. Or, an extra div wrapper if using OPs example. Copying easiest of course in regards to restoring etc.

    This approach can be useful in some cases.

    Below is a simple example where rows are moved when filtered. And yes, it is ranking of stripper names, found it fitting as we are talking stripes ... hah

    const Filter = {
      table: null,
      last: {
        tt: null,
        value: ''
      },
      name: function (txt) {
        let tb_d = Filter.table.querySelector('.data'),
            tb_f = Filter.table.querySelector('.filtered'),
            tr = tb_d.querySelectorAll('TR'),
            f = 0
        ;
        tb_f.innerHTML = '';
        if (txt.trim() == '') {
          tb_d.classList.remove('hide');
        } else {
          txt = txt.toLowerCase();
          for (let i = 0; i < tr.length; ++i) {
            let td = tr[i].querySelectorAll('TD')[1];
            if (td.textContent.toLowerCase().includes(txt)) {
              tb_f.appendChild(tr[i].cloneNode(true));
              f = 1;
            }
          }
          if (f)
            tb_d.classList[f ? 'add' : 'remove']('hide');
        }
      },
      key: function (e) {
        const v = e.target.value;
        if (v == Filter.last.value)
          return;
        Filter.last.value = v;
        clearTimeout(Filter.last.tt);
        Filter.last.tt = setTimeout(function () { Filter.name(v); }, 200);
      }
    };
    
    Filter.table = document.getElementById('table');
    Filter.table.addEventListener('keyup', Filter.key);
    table {
      width: 200px;
      border: 3px solid #aaa;
    }
    tbody tr { background: #e33; }
    tbody tr:nth-child(even) { background: #e3e;  }
    
    .hide { display: none; }
    #Name
    1Crystal
    2Tiffany
    3Amber
    4Brandi
    5Lola
    6Angel
    7Ginger
    8Candy

提交回复
热议问题