简体   繁体   English

Javascript异常:未处理的方法size()

[英]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. 我使用JQuery的表排序器对表进行排序,但是我遇到了一个空表引发异常的问题。 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() JavaScript执行错误:对象无法处理属性或方法size()

(Translated from French) (从法语翻译)

So I tried using .length method instead, to no avail 所以我尝试使用.length方法,但无济于事

$(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 JavaScript执行错误:无法从空引用获取属性“ 0”

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 jQuery的size()在版本1.8中已弃用,并在jQuery 3.0中被完全删除,这仅仅是因为native length 属性执行相同的操作

$(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. 第二个错误与发布的代码无关,除非您实际上错过了tablesorter函数和if条件的结尾。

Most likely it's the tablesorter plugin not receiving the correct arguments 很可能是tablesorter插件未收到正确的参数

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

Try this, using children() method instead. 请尝试使用children()方法代替。 I hope it will work. 我希望它能起作用。 Thanks. 谢谢。

maybe when you are using some properties,you can try it in chrome! 也许当您使用某些属性时,可以在chrome中尝试! like, this: 像这样:

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

});

and, you will know using the right property! 并且,您将知道使用正确的属性!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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