我想将数组发送为eq函数作为参数。像那样:
$(this).find('tr').not(':eq(array)').each(function(){
});
Run Code Online (Sandbox Code Playgroud)
我通过使用循环和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); \
});\
}\
});");
}
Run Code Online (Sandbox Code Playgroud)
有什么办法吗?
我假设您的数组包含一组代表元素索引的数字。该eq选择不与任何工作。
您可以filter用来将匹配的元素集减少为数组中索引处的元素集:
var arr = [1, 2];
$("someSelector").filter(function(index) {
return arr.indexOf(index) > -1;
});
Run Code Online (Sandbox Code Playgroud)
这是一个有效的例子。
请注意使用Array.prototype.indexOf,这在旧版浏览器(值得注意的IE <版本9)中不可用。但是,有很多垫片可以解决该问题。或者,如注释中所述(感谢@mcgrailm),您可以使用jQuery.inArray:
var arr = [1, 2];
$("someSelector").filter(function(index) {
return $.inArray(index, arr) > -1;
});
Run Code Online (Sandbox Code Playgroud)