gpt4 book ai didi

jquery数组到eq函数

转载 作者:行者123 更新时间:2023-12-01 01:03:16 25 4
gpt4 key购买 nike

我想将数组作为参数发送到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com