1

I was using the following jquery code to select the first 10 consecutive elements

$(".pag-num-1").nextAll(".num-btn").slice(0, 9).show();

The problem was, it wasn't selecting the base element i.e. .pag-num-1 element, so what I am doing now is manually displaying the element .pag-num-1 i.e.

$(".pag-num-1").nextAll(".num-btn").slice(0, 10).show();
$(".pag-num-1").show();

I wasn't able to find another to achieve this.

Is there another way that I can select the base element i.e. .pag-num-1 element along with the other elements i.e. using the same line in which I have used nextAll()? That'd be more cleaner, wouldn't it?

4

1 回答 1

3

您可以使用addBack()

$(".pag-num-1").nextAll(".num-btn").addBack().slice(0, 10).show();

这将创建一个新的 jQuery 对象,其中包含 的结果nextAll()和最初匹配的元素($(".pag-num-1")在我们的例子中)。

注意:在 jQuery 1.8 之前addBack()命名。andSelf()

于 2013-08-12T08:15:50.703 回答