Я собираюсь принять следующую разметку ... вы можете адаптировать ее к своей собственной:
<div id="images">
<div>
<img src="..." />
<em>Year: </em><span class="year">2007</span>
<em>Color: </em><span class="color">red</span>
<em>Type: </em><span class="type">portrait</span>
</div>
<div>
<img src="..." />
<em>Year: </em><span class="year">2008</span>
<em>Color: </em><span class="color">yellow</span>
<em>Type: </em><span class="type">panoram</span>
</div>
</div>
Тогда вам понадобится jQuery:
function sortBy(field) {
var container = $('#images');
// get a real array of the image divs
var imgDivList = container.children().get();
// Put them into the correct order based on the chosen field
imgDivList.sort(function (a, b) {
var aText = $(a).children('.' + field).text();
var bText = $(b).children('.' + field).text();
if (aText < bText) {
return 1;
}
if (aText > bText) {
return -1;
}
if (aText == bText) {
return 0;
}
});
// Append them back to the original container
container.append(imgDivList);
}
Для фильтрации сделайте следующее:
function filter(field, value) {
var imgDivList = $('#images').children();
imgDivList.each(function (index, element) {
if ($(element).children('.' + field).text() == value) {
$(element).show();
} else {
$(element).hide();
}
});
}