简体   繁体   中英

Javascript Exception : unhandled method size()

I use JQuery's tablesorter to sort my tables, but I'm having an issue with empty tables throwing exceptions. So I added a condition to my script, but now the problem comes from the condition. :

$(document).ready(function ($) {
    if($("#printerTable").find("tbody").size()>0){
        $("#printerTable").tablesorter({ sortList: [[0, 0], [1, 0], [2, 0]] 
});

The exception is :

Javascript execution error : The Object doesn't handle property or method size()

(Translated from French)

So I tried using .length method instead, to no avail

$(document).ready(function ($) {
    if($("#printerTable").find("tbody").length > 0){
        $("#printerTable").tablesorter({ sortList: [[0, 0], [1, 0], [2, 0]] });
    }
});

The exception becomes :

JavaScript Execution error : impossible to obtain property "0" from a null reference

Can you see what I'm not seeing here? Thanks!

jQuery's size() was deprecated in version 1.8 and removed completely in jQuery 3.0, simply because the native length property does the same thing

$(document).ready(function() {
    if ( $("#printerTable").find("tbody").length > 0 ) {
        $("#printerTable").tablesorter({ 
            sortList: [[0, 0], [1, 0], [2, 0]]
        });
    }
});

The second error isn't really related to the posted code, unless you're actually missing the closing to the tablesorter function and the if condition.

Most likely it's the tablesorter plugin not receiving the correct arguments

$('#printerTable tbody').children().length;

Try this, using children() method instead. I hope it will work. Thanks.

maybe when you are using some properties,you can try it in chrome! like, this:

$(document).ready(function ($) {
    console.dir($("#printerTable"));
    //you will see all the properties! 

});

and, you will know using the right property!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM