开发者

Javascript: Sort links by content that are inside a div

I have a div that contains a set of links and my goal is to automatically sort those links开发者_高级运维 by content, follow the examples below for better understanding:

before

<div id="sort-this-div">
<a href="http://something45yer.com">Content3</a>
<a href="http://somethingeyerty.com">Content1</a>
<a href="http://somethingfwegrw.com">Content2</a>
<a href="http://somethingt43rwer.com">Content4</a>
</div>

after

<div id="sort-this-div">
<a href="http://somethingeyerty.com">Content1</a>
<a href="http://somethingfwegrw.com">Content2</a>
<a href="http://something45yer.com">Content3</a>
<a href="http://somethingt43rwer.com">Content4</a>
</div>


Assuming a jQuery-like environment:

sorted = $('#sort-this-div a').sort(function(a, b) {return a.innerHTML > b.innerHTML});
$('#sort-this-div').html('');
sorted.each(function(i, a) {$('#sort-this-div').append(a)});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜