【发布时间】:2011-07-11 19:19:23
【问题描述】:
简化此代码的最佳方法是什么。我考虑过使用 .contains 但我不确定如何。如果您需要更多代码,请告诉我。
谢谢。
$.each(catalog.products,
function(index, value) {
if (filterValue == '' || value.name.toUpperCase().indexOf(filterValue.toLocaleUpperCase()) != -1) {
items.push('<li id="' + index + '">' +
'<a data-identity="productId" href="./details.page?productId=' + index + '" >' +
'<img class="ui-li-thumb" src="' + value.thumbnail + '"/>' +
'<p>' + value.brand + '</p>' +
'<h3>' + value.name + '</h3>' +
'<span class="ui-li-count">' + value.price + ' $</span></li>') +
'</a>';
}
else if (filterValue == '' || value.brand.toUpperCase().indexOf(filterValue.toLocaleUpperCase()) != -1) {
items.push('<li id="' + index + '">' +
'<a data-identity="productId" href="./details.page?productId=' + index + '" >' +
'<img class="ui-li-thumb" src="' + value.thumbnail + '"/>' +
'<p>' + value.brand + '</p>' +
'<h3>' + value.name + '</h3>' +
'<span class="ui-li-count">' + value.price + ' $</span></li>') +
'</a>';
}
}
);
【问题讨论】:
-
它们都是一样的,除了第二个说 value.brand 而不是 value.name
-
if (a||b){X} else if (a||c){X}等价于if(a||b||c){X}
标签: javascript jquery arrays list