'.sort' not working on Safari

只愿长相守 提交于 2021-01-28 22:10:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!