我正在使用jQuery Lazy plugin用于延迟加载 JS 和 CSS 资源。我也在使用TableSorter jQuery plugin用于使表格可排序。现在,当我简单地在 Lazy 插件中定义 Tablesorter 然后像这样使用 Tablesorter 时,这两个工作得很好,没有问题:
$('#table_id').tablesorter();
上面的内容按预期工作,Tablesorter 库按需加载,并且 HTML 表格变得可排序。但是,我需要将自定义解析器添加到表排序器中,如下所示:
$.tablesorter.addParser({
id: 'my_parser',
is: function(s) {
return false;
},
format: function(s) {
var result = s.match(/([0-9]+)/);
if (result == null) return 0;
return parseInt(result[0]);
},
type: 'numeric'
});
上述 addParser 代码的问题是,我收到错误“$.tablesorter 没有方法 'addParser'”。无论 TableSorter 插件是否已加载,都没有什么区别。惰性插件用它自己的函数覆盖 $.tablesorter 并且不允许这个调用之王 - 我想。
示例 behaviour can be seen here in this jsFiddle .
有人可以帮助我吗?我非常感谢任何帮助或想法。预先感谢您。
最佳答案
只是为了证明我的观点yepnope.js可以轻松地完成您需要的操作(即,如果周围有任何可排序的表格,则有条件加载表格排序器插件):
$(function () {
yepnope({
test : $("table.sortable").length,
yep : "/resources/jquery.tablesorter.js",
complete: function () {
$.tablesorter.addParser({
// whatever;
});
$("table.sortable").tablesorter();
}
});
});
关于jquery - 使用 jQuery 延迟加载插件和 tablesorter 插件与 addParser 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8550706/