1. 程式人生 > >JS實現根據英文字母排序

JS實現根據英文字母排序

cti png itl doctype pan on() blog set emp

技術分享


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-2.1.4.min.js"></script>
</head>
<body>
<select id="myselect" size="5">
<option>E</option>
<option>D</option>

<option>C</option>
<option>A</option>
<option>B</option>
</select>
<script>
var options={};
$(‘#myselect option‘).each(function(){
var option = $(this);
options[option.text()] = option;
});
var keys = Object.keys(options).sort();//排序

var $myselect = $(‘#myselect‘).empty();//移除所有內容
$.each(keys,function(i,key){
$myselect.append(options[key]);
});

</script>
</body>
</html>

JS實現根據英文字母排序