Difference between $('#tabs a') and $('#tabs').find('a')

MatanyaAnsuman Bebarta提出了一个问题:Difference between $('#tabs a') and $('#tabs').find('a'),或许与您遇到的问题类似。

回答者Matanya给出了该问题的处理方式:

$("#tabs a") evaluates from right to left - which is the native direction of both Sizzle selector engine and querySelectorAll - i.e. first it finds all of the anchor elements in the page and then narrows it down to those under #tabs.

$("#tabs").find("a") evaluates - more intuitively - from left to right, i.e. first it finds #tabs, and then only the anchor elements under it.

Clearly the latter would yield better performance, but it would only be noticeable accumulatively; that is, if you run thousands of queries. Otherwise, the difference is negligible.

希望本文对你有帮助,欢迎支持JavaScript中文网