私の HTML には、それぞれが内部に名前を持つ多数の div があります。アルファベットのフィルターを実装する必要があります。ユーザーがボタン「AJ」をクリックすると、リストは、名前の最初の文字が AJ の間にある div のみを作成し、他の文字グループについても同様です。私は今まで次のコードを書いています:
$("#jfmfs-filter-selected-aj").click(function(event) {
event.preventDefault();
for (i = 0; i < all_friends.length ; i++)
{
var aFriend = $( all_friends[i] );
if(!(/^[a-jA-J]+$/.test(aFriend.find(".friend-name").html().charAt(0)))){
aFriend.addClass("hide-filtered");
}
}
$(".filter-link").removeClass("selected");
$(this).addClass("selected");
});
このコードは正常に機能しますが、ループ内の div を非表示にするため、時間がかかります。上記のコードを1行で記述して、基準を満たすすべてのdivに「hide-filtered」クラスを一度に追加する方法はありますか?
all_friends.not( {divs with inner html starting with letters a-j} ).addClass("hide-non-selected");
all_friends.not( /^[a-jA-J]+$/.test(all_friends.find(".friend-name").html().charAt(0)) ).addClass("hide-non-selected");
jqueryフィルターを使用して使用した最終的な解決策(Barmarの回答):
all_friends.filter(function() {
return(!(/^[a-fA-F]+$/.test($(this).find(".friend-name").html().charAt(0))))
}).addClass("hide-non-selected");