第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Foreach 僅循環(huán)通過表的最后一列 (ES6)

Foreach 僅循環(huán)通過表的最后一列 (ES6)

炎炎設(shè)計 2021-11-25 16:25:58
我試圖為電話列表構(gòu)建我的第一個搜索功能。不幸的是,我的過濾器函數(shù)僅通過表格的最后一列循環(huán)。我錯過了什么?或者我必須為此使用不同的方法嗎?PS:請原諒可能的重復(fù)。我發(fā)現(xiàn)的所有示例都是針對 PHP 的。提前謝謝了!const phonelist = document.querySelector('table');const searchInput = document.querySelector('#search');const searchResult = document.querySelector('#search-result');const searchValue = document.querySelector('#search-value');// EVENTSfunction initEvents() {  searchInput.addEventListener('keyup', filter);}function filter(e) {  let text = e.target.value.toLowerCase();  console.log(text);  // SHOW SEARCH-RESULT DIV  if (text != '') {    searchValue.textContent = text;    searchResult.classList.remove('hidden');  } else {    searchResult.classList.add('hidden');  }  document.querySelectorAll('td').forEach((row) => {    let item = row.textContent.toLowerCase();    if (item.indexOf(text) != -1) {      row.parentElement.style.display = 'table-row';      console.log(row.parentElement);    } else {      row.parentElement.style.display = 'none';    }  })}// ASSIGN EVENTSinitEvents();<input id="search" /><div class="phonelist">  <div id="search-result" class="hidden">    <p>Search results for <b id="search-value"></b>:</p>  </div>  <table class="striped">    <thead>      <tr>        <th>Phone</th>        <th>Fax</th>        <th>Room</th>        <th>Name</th>        <th>Title</th>      </tr>    </thead>    <tbody>      <tr>        <td>165</td>        <td>516</td>        <td>1.47</td>        <td>Johnathan Doe</td>        <td>Sales</td>      </tr>      <tr>        <td>443</td>        <td>516</td>        <td>1.47</td>        <td>Jane Dow</td>        <td>Development</td>      </tr>    </tbody>  </table></div>
查看完整描述

2 回答

?
白板的微信

TA貢獻(xiàn)1883條經(jīng)驗 獲得超3個贊

看起來您正在查詢錯誤的元素

  document.querySelectorAll('td').forEach((row) => {

我想你想查詢行

  document.querySelectorAll('tr').forEach((row) => {

否則,無論最后一列的結(jié)果如何,您都將覆蓋您的班級更改

(并且顯然將類應(yīng)用于 tr 而不是 tr 的父級)


查看完整回答
反對 回復(fù) 2021-11-25
?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗 獲得超6個贊

您的代碼實際上正在遍歷所有元素,但最后一列的更改覆蓋了前一列的更改。


假設(shè)您搜索了dow,第 2 行第 4 列匹配并顯示父行,但之后您的循環(huán)轉(zhuǎn)到不匹配的第 2 行第 5 列并隱藏父行。


我已經(jīng)更新了您的代碼,如下所示,您應(yīng)該遍歷行,檢查其任何列是否匹配,并根據(jù)結(jié)果僅更新該行一次。


const phonelist = document.querySelector('table');

const searchInput = document.querySelector('#search');

const searchResult = document.querySelector('#search-result');

const searchValue = document.querySelector('#search-value');


// EVENTS

function initEvents() {

  searchInput.addEventListener('keyup', filter);

}


function filter(e) {

  let text = e.target.value.toLowerCase();

  console.log(text);


  // SHOW SEARCH-RESULT DIV

  if (text != '') {

    searchValue.textContent = text;

    searchResult.classList.remove('hidden');

  } else {

    searchResult.classList.add('hidden');

  }


  document.querySelectorAll('tr').forEach(row => {

    let foundMatch = false;


    row.querySelectorAll('td').forEach(col => {

      let item = col.textContent.toLowerCase();

      foundMatch = foundMatch || item.indexOf(text) > -1;

    });


    if (foundMatch) {

      row.style.display = 'table-row';

    } else {

      row.style.display = 'none';

    }

  });

}


// ASSIGN EVENTS

initEvents();

<input id="search" />


<div class="phonelist">

  <div id="search-result" class="hidden">

    <p>Search results for <b id="search-value"></b>:</p>

  </div>

  <table class="striped">

    <thead>

      <tr>

        <th>Phone</th>

        <th>Fax</th>

        <th>Room</th>

        <th>Name</th>

        <th>Title</th>

      </tr>

    </thead>

    <tbody>

      <tr>

        <td>165</td>

        <td>516</td>

        <td>1.47</td>

        <td>Johnathan Doe</td>

        <td>Sales</td>

      </tr>

      <tr>

        <td>443</td>

        <td>516</td>

        <td>1.47</td>

        <td>Jane Dow</td>

        <td>Development</td>

      </tr>

    </tbody>

  </table>

</div>


查看完整回答
反對 回復(fù) 2021-11-25
  • 2 回答
  • 0 關(guān)注
  • 236 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號