したがって、このような順序付けされていないリスト内にリストアイテムのコレクションがあります。
<div class="some-input-field-class">
<input type="text" name="filter" id="filter">
<label for="filter">Filter Names</label>
</div>
<ul class="my-user-collection">
<li class="thumb selectable arrow light" style="margin-bottom:-5px;"
data-image="http://cdn.tapquo.com/lungo/icon-144.png">
<strong class="name">Peter <font data-count="0" style="position:relative;top:-2px;"> </font></strong>
<small class="description">Hi!</small>
</li>
...
</ul>
これをjavascriptファイルに追加できます。ただし、イベントリスナーもロードする必要があります。おそらくいくつかのイベントがあるので、必要なことを実行してください。たぶん、残りのイベントリスナーをロードする別の関数に追加します。
// Load the event listener
document.querySelector('#filter').addEventListener('keyup', filterNames);
クラスをターゲットにしてliを反復処理する関数。getElementByIDではなくquerySelectorAllを使用するため、配列を反復処理します。-1は一致しないことを意味します。
function filterNames(e) {
const text = e.target.value.toLowerCase();
document.querySelectorAll('.selectable').forEach(
function(name) {
let item = name.firstChild.textContent;
if (item.toLowerCase().indexOf(text) != -1) {
name.style.display = 'block';
} else {
name.style.display = 'none';
}
}
);
}