0
有沒有一種更有效的方法來做下面的JavaScript的一點?我很想削減這個,但想不到一個辦法。謝謝!
var sizes = []; //This is a dynamic array from button clicks in an above menu that will include any of ['S','M','L','XL']
sizes = ['S','M'] // We'll use this as an example saying that the S and M buttons are active.
$('div').each(function() {
var t = $(this);
var mysizes = t.attr('class').split(/\s+/);
var length = sizes.length;
if(length == 0) {
t.show();
}
if(length == 1) {
if(t.hasClass(sizes[0])) {
t.show();
} else {
t.hide();
}
}
if(length == 2) {
if(t.hasClass(sizes[0]) || t.hasClass(sizes[1])) {
t.show();
} else {
t.hide();
}
}
if(length == 3) {
if(t.hasClass(sizes[0]) || t.hasClass(sizes[1]) || t.hasClass(sizes[2])) {
t.show();
} else {
t.hide();
}
}
if(length == 4) {
if(t.hasClass(sizes[0]) || t.hasClass(sizes[1]) || t.hasClass(sizes[2]) || t.hasClass(sizes[3])) {
t.show();
} else {
t.hide();
}
}
});
任何幫助將不勝感激。
我不得不如此接近這一概念的東西,但無法得到它的工作。這是完美的,謝謝! – 2013-05-09 23:22:34