jquery数组到eq函数

标签 jquery arrays

我想将数组作为参数发送到 eq 函数中。像这样:

$(this).find('tr').not(':eq(array)').each(function(){

});

我使用循环和 eval 函数来完成此操作,但它看起来并不容易编辑。这是我的代码。

$.fn.grilestir = function(options){

var nots = '';

for(var i=0;i<options.row_numbers.length;i++){
    nots += "not(':eq("+options.row_numbers[i]+")').";
}

    eval("$(this).find('tr')."+nots+"each(function(){\
        var tr = $(this); var orj;\
        if(options.mod == 'passive-rows'){\
            $(this).mouseover(function(){\
                orj = tr.css('backgroundColor');\
                tr.css('backgroundColor', '#777777');\
            });\
            $(this).mouseout(function(){\
                tr.css('backgroundColor', orj); \
            });\
        }\
    });");

}

有什么办法可以做到这一点吗?

最佳答案

我假设您的数组包含一组表示元素索引的数字。 eq 选择器将无法使用它。

您可以使用filter将匹配的元素集减少到数组中索引处的元素:

var arr = [1, 2];
$("someSelector").filter(function(index) {
    return arr.indexOf(index) > -1;
});

这是一个working example .

请注意 Array.prototype.indexOf 的使用,它在较旧的浏览器(尤其是 IE < 版本 9)中不可用。然而,有很多垫片可以解决这个问题。 或者,如评论中所述(感谢@mcgrailm),您可以使用 jQuery.inArray :

var arr = [1, 2];
$("someSelector").filter(function(index) {
    return $.inArray(index, arr) > -1;
});

关于jquery数组到eq函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9278227/

相关文章:

c# - 强制对数组进行垃圾回收,C#

arrays - 声明一个常量数组

c - 删除 1 0's from an array, replaces 10' 和 0 的函数。将所有剩余元素移至数组开头

java - 为什么服务器端数据表在一页中显示所有记录并且搜索不起作用?

javascript - 无法使用 CSS colgroup 保持表格列宽固定

javascript - 触发单选按钮单击并设置选中的属性问题

arrays - Perl Data::Dumper 数组内的哈希值?

javascript - 如何在 JavaScript 中获取单词或索引后的子字符串?

javascript - IE 错误触发点击 2 个按钮?

javascript - 来自 jQuery Mobile 的面板