按背景网址按字母顺序排列

时间:2013-02-02 17:06:51

标签: jquery

尝试通过href网址对其进行字母顺序修改,而不是使用href文本...

的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);
}

$(function () { sortUsingNestedText($('.searchresults'), "div", ".title a"); });

HTML

<div class="title"><a href="../url/thisiswhatiwantsorted/default.html">Link</a></div>

1 个答案:

答案 0 :(得分:0)

改变这个:

var vA = $(keySelector, a).text();
var vB = $(keySelector, b).text();

为:

var vA = $(keySelector, a).attr("href");
var vB = $(keySelector, b).attr("href");

并按网址的最后一部分排序:

var vAURL = $(keySelector, a).attr("href").split('/');
var vA = vAURL[vAURL.length-1];
var vBURL = $(keySelector, b).attr("href").split('/');
var vB = vBURL[vBURL.length-1];