开发者

Javascript: Sort links by content that are inside a div

开发者 https://www.devze.com 2023-01-29 22:32 出处:网络
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:

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

精彩评论

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