Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于行索引数组获取jQuery行集合_Javascript_Jquery_Html - Fatal编程技术网

Javascript 基于行索引数组获取jQuery行集合

Javascript 基于行索引数组获取jQuery行集合,javascript,jquery,html,Javascript,Jquery,Html,我有一个索引数组(没有特定的顺序),我需要根据这些索引选择表中的所有 我所得到的感觉是草率和低效的。是否有更好的方法编写此选择器 对于喜欢小提琴的人: 对于其他人: <table> <tr> <td>one</td> </tr> <tr> <td>two</td> </tr> <tr> &l

我有一个索引数组(没有特定的顺序),我需要根据这些索引选择表中的所有

我所得到的感觉是草率和低效的。是否有更好的方法编写此选择器

对于喜欢小提琴的人:

对于其他人:

<table>
    <tr>
        <td>one</td>
    </tr>
    <tr>
        <td>two</td>
    </tr>
    <tr>
        <td>three</td>
    </tr>
    <tr>
        <td>four</td>
    </tr>
    <tr>
        <td>five</td>
    </tr>
    <tr>
        <td>six</td>
    </tr>
    <tr>
        <td>seven</td>
    </tr>
</table>

var indices = [0,3,4,6];
//What I'd love to do
alert($("tr").eq(indices).length); //returns 0

//What does work, but feels sloppy & inefficient 
var selector = ""; 
$(indices).each(function(i, index){
    selector += "tr:eq(" + index + ")";
    if(i + 1 != indices.length){
        selector += ","
    }
});

alert($(selector).length); //returns 4

一
二
三
四
五
六
七
var指数=[0,3,4,6];
//我想做什么
警报($(“tr”).eq(指数).length//返回0
//什么有用,但感觉马虎和低效
var选择器=”;
$(索引)。每个(函数(i,索引){
选择器+=“tr:eq(“+索引+”);
如果(i+1!=索引长度){
选择器+=“,”
}
});
警报($(选择器).length)//返回4
如有任何帮助、意见和建议,将不胜感激

谢谢

试试这个

var indices = [0,3,4,6];

$( "table > tr").filter( function(index){
   return $.inArray(index, indices) > -1; 
});

杰出的我知道一定有更好的办法。谢谢!
var indices = [0,3,4,6];

$( "table > tr").filter( function(index){
   return $.inArray(index, indices) > -1; 
});