更新2:
どうもありがとうございました。3つのソリューションはすべて機能しましたが、読みやすさとパフォーマンスの点でBillが気に入っています。いつものように、私は専門知識のレベルに驚いており、ここで助けてくれます。本当に助けに感謝します。
アップデート:
デモをjsfiddleに載せる:http://jsfiddle.net/FC4QE/17/
フィルタを作成する必要があります。ユーザーがブランド名のリンクをクリックし、一致するものがある場合は、他の製品を除外する必要があります。ブランドは商品名に含まれているので、一致するものを探しています。1つまたは複数ある場合は、他の商品を非表示にする必要があります。
私は次のjavascipt/jqueryコードを持っています:
$(function(){
$('#filter-by-brand li a').click(function(){
// get html string of clicked item
var brandNameSelected = $(this).html();
var productContainer = $('#product-collection div.productBoxWrapper');
// reset products in the view
if (brandNameSelected == 'All Brands'){
productContainer.fadeIn("slow");
}
// for each product title (a tag)
$("#product-collection h4 a").each(function(){
var productTitle = jQuery(this).html();
// if item clicked is contained inside product title, hide all
// products and only show the ones where the title matched
if(productTitle.indexOf(brandNameSelected) != -1){
// hide container of all products, this hides all products
productContainer.fadeOut("slow");
// then show only ones that match. the problem is that only the one product is
// displayed since we're inside the .each. How can I show all products where product title contains the item clicked?
$(this).parent().parent().parent().parent().fadeIn("slow");
}
});
});
});
コード内のコメントですべてを説明しましたが、基本的に、コードは機能しますが、クリックされたアイテムが.eachメソッド内に含まれている製品を表示しているため、最後に一致したアイテムのみが表示されます。.each内で一致するものをすべて表示するにはどうすればよいですか、またはこれは不可能であり、別の方法はありますか?
これが理にかなっていて、誰かがアドバイスをくれることを願っています!ありがとう。