javascript - Eval 函数或 document.write

标签 javascript jquery

我使用插件http://tablesorter.com/docs/为了在javascript中对我的表格进行排序并过滤列,我的问题是我不想在所有列上放置过滤器,因此我使用这个:

           widgetOptions : {
                filter_formatter : {
                    1:function($cellnindx){return null;},
                    4:function($cell,indx){return null;}
                }
            }

它可以工作,但现在我想做一个脚本,不要在具有 notFiler 类的列上打印过滤器,所以我执行此脚本:

        var temp = "";
        var i = 0;
        $(".tablesorter th").each(function()
        {
            if($(this).attr('class') == "notFiler")
            {
                if(temp != "")
                {
                    temp += ',';
                }
                temp += i + ': function($cell, indx){return null;}';
            }
            i++;
        });

它允许我生成文本,我允许我不在某些列上打印过滤器,但当我这样做时:

        widgetOptions : {
            filter_formatter : {
                eval(temp);

            }
        }

我有一个 JavaScript 错误:意外的 token ;

最佳答案

不要使用评估。难道你就不能简单地这样做吗?

首先创建结果对象

var widgetOptions = {
    filter_formatter: {};
};

现在只需附加到它

$(".tablesorter th").each(function(i) {
    if($(this).attr('class') == "notFiler") {
        widgetOptions.filter_formatter[i] = function($cell, indx){return null;};
    }
});

然后,初始化表排序器

var tablesorter = $('.tablesorter').tablesorter({
    widgetOptions: widgetOptions
});

关于javascript - Eval 函数或 document.write,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29847100/

相关文章:

javascript - 使用 Istanbul 尔的 nightwatch.js 的代码覆盖率

javascript - Jquery 和 Angularjs

javascript - 具有相同类名的 Div : Select First Child and Set Different CSS Styles for Each

Javascript 似乎忽略了我的功能

javascript - Angular 过滤器 - 按对象(或多个字段)分组

javascript - 函数内函数方法

javascript - 使用带递归的 reduce 函数从多级树生成一个平面 id 数组?

javascript - form.submit 方法在 jQuery 中不起作用

jquery - 使用 IP 的 getJSON 不起作用

javascript - 如何(某种程度上)向 fullcalendar 的 renderEvents 处理程序添加代码?