Q:

How to select div1~divN,where N is a variable with jQuery?

<div class="target"> </div>
...
<div class="target"> </div>

$('div.target:eq(0),...div.target:eq(N-1)') only works when N is a constant,but in my case N is a variable..

A:

A very fast way is to use .slice().
$('div.target').slice( 0, N );

This will be faster than using selectors, especially since selectors like :lt() are not valid CSS selectors, and as such are not useful for querySelectorAll, which jQuery (Sizzle) utilizes whenever possible.

A:

Use the lt selector.
$('.target:lt(' + N + ')');

