達令說
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;
添加回答
舉報
0/150
提交
取消