How to tell jQuery to stop searching DOM when the first element is found?

前端 未结 3 1178
滥情空心
滥情空心 2020-12-29 04:28

From what I understand, adding .first() or :first to a query doesn\'t stop DOM search after the first match. It just tells jQuery to take 1st eleme

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 05:12

    :first matches only a single element. its equivalent to :eq(0).

    .first() reduces the set of matched elements to the first in the set.

    so using :first would seem like its what you need, but on looking closer they're both using selectors first to find a set and then going on to use the first.

    There's some comments and discussion here and here that seems to indicate that :first() doesn't stop at the first element.

    References:

    http://api.jquery.com/first-selector/

    http://api.jquery.com/first/

提交回复
热议问题