私が探している機能は「ライブフィルター」と呼ばれ、出力されたデータベースエントリをフィルター処理しようとしています。各エントリの構造は次のとおりです。
<div class="class1 class2 class3" data-name="name" data-date="MM/DD/YY">
<img src="path/to/image.jpg">
<div class="class4">
<h4>some text here</h4> <!-- this is what i want to search in -->
<div class="class5">
<span id="date">date: MM/DD/YY</span>
<span id="id_one">text</span>
</div>
<div class="class6">
<span id="id_two">text</span>
<span id="id_three">text</span>
</div>
<span id="id_four">text</span>
<span id="id_five"><a class= class7 href="path/to/link.html">link</a></span>
</div>
</div>
.class4 h4内を検索してから.class1
、検索したテキストを含まないすべてのdivをフェードアウトしたいと考えています。
これは私が遭遇したソリューション(主にここ)のデフォルトの動作ですが、それらはリストアイテムでのみ機能し、divでは機能しないようです。たとえば、ここからコードを試し、次のように変更しました。
<script>
$(document).ready(function() {
$("#filter").keyup(function() {
// Retrieve the input field text and reset the count to zero
var filter = $(this).val(), count = 0;
// Loop through the comment list
$(".class4 h4").each(function() {
// If the list item does not contain the text phrase fade it out
if ($(this).text().search(new RegExp(filter, "i")) < 0) {
$(".class1").fadeOut();
// Show the list item if the phrase matches and increase the count by 1
}
else {
$(this).show();
count++;
}
});
// Update the count
var numberItems = count;
$("#filter-count").text("# of matches = "+count);
});
});
</script>
正しく検索されましたが、すべてのdivが。で非表示になりまし.class1
た。
ライブフィルターを使用して特定のテキストを検索し、検索したテキストを含まない .class4 h4
divをフェードアウトする方法を知っている人はいますか?.class1
ありがとうございました。