亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

過濾器未顯示正確的類別

過濾器未顯示正確的類別

達令說 2023-09-28 17:32:36
我需要根據最后一列制作工作過濾器。最后一列是類別,當我按選項>選擇時,我需要只顯示一個類別并隱藏其他類別,但現在當我單擊時什么也沒有發生,也不知道為什么。highlightRows = () => {    let oddRows = document.querySelectorAll('tbody > tr.show')    oddRows.forEach((row, index)=> {        if (index % 2 == 0) {            row.style.background = '#f1f1f1'        } else {            row.style.background = '#fff'        }    })}const filterOptions = () => {    const option = document.querySelector("#filter").value;    const selection = option.replace('&', '')  const rows = document.querySelectorAll("#body1 > tr");  console.log(rows.length);        rows.forEach(row => {        let td = row.querySelector("td:last-child");        let filter = td.innerText.replace('&', '');        if (filter === selection) {            row.className = 'show'        } else {            row.className = 'hidden'        }    });    highlightRows()};document.getElementById("filter").addEventListener("change", filterOptions); <div class="table-filters">        <select id="filter">          <option disabled selected value="none">Categories</option>          <option>Home</option>          <option>Others</option>          <option>Hobby</option>          <option>Garden</option>        </select>      </div>      <table class="vypis">        <caption>Account</caption>        <thead>          <tr>            <th scope="col">Referencia</th>            <th scope="col">Dátum</th>            <th scope="col">Suma</th>            <th scope="col">Kategória</th>          </tr>        </thead>        <tbody class="body1">          <tr class="vypis-riadok">            <td scope="row" data-label="Referencia">[[X04_textovy_popis_obycajne]]</td>            <td data-label="Dátum">[[X02_riadok_1_datum]]</td>            <td data-label="Suma">[[X08_riadok_1_suma]] €</td>            <td data-label="Kategória">[[kategoria]]</td>          </tr>          </tr>        </tbody>      </table>
查看完整描述

2 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

問題出在你的查詢中,tbody你寫的#body1所以它會查詢帶有 ID 的元素,body1而在你的 html 代碼中,tbody是類body1而不是 id


const rows = document.querySelectorAll("#body1 > tr"); // <--- will select element with id="body1"

你的 HTML 代碼:


   ...

   </thead>

    <tbody class="body1"> <!--body is using attribute class -->

      <tr class="vypis-riadok">

   ...

你應該做的是使用類查詢選擇器,更改#為.


const rows = document.querySelectorAll(".body1 > tr"); // <--- will select element with class="body1"

之后你的 javascript 代碼應該沒問題,現在添加 css 樣式show和hidden類


.hidden{

 display: none;


查看完整回答
反對 回復 2023-09-28
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

應該是因為我通過ID定義了選擇



查看完整回答
反對 回復 2023-09-28
  • 2 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號