<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8"/> <title>针对选择框词典式排序</title> <script type="text/javascript" src="../js/jquery-1.11.0.js"></script> <script type="text/javascript"> $(function() { /*$(‘button‘).click(function() { var options = $(‘option‘); for (var i = 1; i < options.length; i++) { for (var j = 0; j < i; j++) { var currOption = $(options.get(i)), loopOption = $(options.get(j)); if (currOption.text() < loopOption.text()) { currOption.insertBefore(loopOption); } } } });*/ $(‘button‘).click(function() { var $options = $(‘option‘); $options.sort(function(a, b) { if ($(a).text() > $(b).text()) return 1; else return -1; }); $(‘select‘).html($options); }); }); </script> </head> <body> <button>排序</button> <select> <option>php</option> <option>c</option> <option>javascript</option> <option>css</option> <option>html</option> <option>python</option> </select> </body> </html>
时间: 2024-10-11 10:44:46