问题
Is working fine on Chrome, could someone tell me why it isn't sorting the table on Safari and how to fix it?
I've looked the console, there's no error.
HTML:
<tr>
<td>
<input name="d1" value="01/01/1992">
</td>
</tr>
<tr>
<td>
<input name="d1" value="01/01/1991">
</td>
</tr>
</tbody>
</table>
<button>SORT</button>
jQuery:
$('button').on('click',function(){
sort();
});
function sort() {
$('tBody tr').sort(function(a, b) {
return new Date($(a).find('input[name="d1"]').val()).getTime() > new Date($(b).find('input[name="d1"]').val()).getTime()
}).appendTo('tBody');
}
JsFiddle:
http://jsfiddle.net/nm5vbtdq/1/
回答1:
I believe you need to return a -1/1 instead of a boolean in Safari, see below:
function sort() {
$('tBody tr').sort(function(a, b) {
var result = new Date($(a).find('input[name="d1"]').val()).getTime() > new Date($(b).find('input[name="d1"]').val()).getTime() ? 1 : -1;
return result;
}).appendTo('tBody');
}
来源:https://stackoverflow.com/questions/27320847/sort-not-working-on-safari