这是更新的示例:http: //jsfiddle.net/Jn67S/7/
单击按钮将元素添加/删除到过滤器数组。结果是包含数组中至少一个元素的菜单。
我希望你能用这个做点什么。
编辑:这是代码:
var filterarray = [];
$(".filterbutton").click(function() {
var tempFilter = $(this).attr("value");
if (filterarray.indexOf(tempFilter) == -1) {
filterarray.push(tempFilter);
} else {
filterarray.splice(filterarray.indexOf(tempFilter),1);
}
if (filterarray.length > 0) {
filter();
} else {
showAllElements();
}
});
function filter() {
var selector = "";
for (i = 0; i < filterarray.length; i ++) {
selector += "." + filterarray[i];
if (i!=filterarray.length - 1) {
selector += ", "; // Remove this line if you want results contain all ingredients in filter array
}
}
if (filterarray.length > 0) {
hideAllMenus();
$("#menus").find(selector).show();
}
}
function hideAllMenus() {
$(".menu").hide();
}
function showAllElements() {
$(".menu").show();
}
和 HTML:
<input type="button" value="potato" class="filterbutton"></input>
<input type="button" value="chicken" class="filterbutton"></input>
<input type="button" value="rice" class="filterbutton"></input>
<input type="button" value="cucumber" class="filterbutton"></input>
<input type="button" value="fish" class="filterbutton"></input>
<input type="button" value="ham" class="filterbutton"></input>
<input type="button" value="milk" class="filterbutton"></input>
<div id="menus">
<div class="menu potato chicken rice cucumber fish">Meal 1</div>
<div class="menu ham milk cucumber fish">Meal 2</div>
<div class="menu chicken">Meal 3</div>
<div class="menu potato fish">Meal 4</div>
<div class="menu potato rice cucumber fish">Meal 5</div>
</div>