我已经花了最后一天搜索Stack并且没有找到这个问题的答案,即使有非常相似的解决方案。这是我正在使用的HTML代码:
<div class="titles">
<div class="filename" id="sfilename"><strong>Name</strong></div>
<div class="filesize" id="sfilesize"><strong>Size</strong></div>
<div class="filetype" id="sfiletype"><strong>Type</strong></div>
<div class="filedate" id="sfiledate"><strong>Last Modified</strong></div>
</div>
<div id="sort">
<div class="folder">
<div class="filename">Folder 1</div>
<div class="filesize"> </div>
<div class="filetype"> </div>
<div class="filedate">May 12 2012 11:24:44 AM</div>
</div>
<div class="folder">
<div class="filename">Folder 2</div>
<div class="filesize"> </div>
<div class="filetype"> </div>
<div class="filedate">May 15 2012 09:24:44 AM</div>
</div>
<div class="folder">
<div class="filename">Folder 3</div>
<div class="filesize"> </div>
<div class="filetype"> </div>
<div class="filedate">May 18 2012 12:24:44 AM</div>
</div>
<div class="file">
<div class="filename">File 1</div>
<div class="filesize">7.38 Kb</div>
<div class="filetype">png</div>
<div class="filedate">May 18 2012 08:24:44 AM</div>
</div>
<div class="file">
<div class="filename">File 2</div>
<div class="filesize">58.2 Kb</div>
<div class="filetype">jpg</div>
<div class="filedate">May 16 2012 07:24:44 AM</div>
</div>
<div class="file">
<div class="filename">File 3</div>
<div class="filesize">135.87 Kb</div>
<div class="filetype">mov</div>
<div class="filedate">May 10 2012 05:24:44 AM</div>
</div>
</div>
我希望能够根据#filename,#filesize,#filetype或#filedate div的内容在#sort div中进行排序。
我已经使用Javascript尝试了这个并且它不起作用:
<script type="text/javascript">
function sortUsingNestedText(parent, childSelector, keySelector) {
var items = parent.children(childSelector).sort(function(a, b) {
var vA = $(keySelector, a).text();
var vB = $(keySelector, b).text();
return (vA < vB) ? -1 : (vA > vB) ? 1 : 0;
});
parent.append(items);
}
$('#sfilename').data("sortKey", "#filename");
$('#sfilesize').data("sortKey", "#filesize");
$('#sfiletype').data("sortKey", "#filetype");
$('#sfiledate').data("sortKey", "#filedate");
$("#sfilename").click(function() {
sortUsingNestedText($('#sort'), "div", $("#sfilename").data("sortKey"));
});
$("#sfilesize").click(function() {
sortUsingNestedText($('#sort'), "div", $("#sfilesize").data("sortKey"));
});
$("#sfiletype").click(function() {
sortUsingNestedText($('#sort'), "div", $("#sfiletype").data("sortKey"));
});
$("#sfiledate").click(function() {
sortUsingNestedText($('#sort'), "div", $("#sfiledate").data("sortKey"));
});
</script>