Is there an equivalent to .closest() that searches down the DOM tree instead of up?

后端 未结 6 1281
失恋的感觉
失恋的感觉 2020-12-20 11:15

Is there an equivalent to .closest() that searches down the DOM tree instead of up?

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 11:31

    No, not with jQuery. But with pure DOM, there is for single elements:

    $($element[0].querySelector('.whatever'))
    

    Pick the first (and only?) element from your selection, run the “DOM .find()” and wrap it up again.

    Compared to the other answers here, this is the only (short) way to stop wasting CPU cycles after finding the first match.—It leaves me, once again, with a bitter aftertaste of the thoughtfulness in jQuery. The DOM feels incomplete, too, no doubt, but at least they have the shortcut version of .querySelectorAll().

提交回复
热议问题