Select Element with index greater than 3 and less than 6

后端 未结 2 471
广开言路
广开言路 2021-01-17 20:50

i want to select element that its index is greater than 3 and less than 6 ex: $(\"td:gt(3)\") and $(\"td:lt(6)\") ?

相关标签:
2条回答
  • 2021-01-17 21:38

    make use of slice(start, end)

    $('td').slice(3,6)
    

    documentation

    0 讨论(0)
  • 2021-01-17 21:51

    Just combine the two and it should work:

    $("td:gt(3):lt(6)");

    You can use any number of pseudo-selectors. They will all apply.

    However, note that the slice answer will be far more efficient than this!

    Update

    The above code is wrong. You need to swap round :lt and :gt because after the gt selector is executed the set of matched elements is reduced and the indexes that :lt applies to are different:

    $("td:lt(6):gt(3)");
    

    However, as mentioned above, slice will be better, performance wise. If you're interested in how much better that performance will be, I've put together a quick test. Here's the results (slice is nearly 4 times faster):

    enter image description here

    0 讨论(0)
提交回复
热议问题