Foo
  • The most efficient way to select a set of items

    For example, if I have an unordered list, let's say

    <ul>
        <li class="foo">Foo</li>
        <li class="bar">Bar</li>
    </ul>
    

    I can choose foo as $('ul li.foo'), $('li.foo')or $('.foo').

    Then which method is more effective (if any) and why. Or is it an indication of parents when choosing children for the only reason, to remove any conflict if there are other elements that have the same class name!

    +3
    source share
    2 answers

    A simple class selector .foowill be the fastest in modern browsers, as it can use its own getElementsByClassName, if available.

    Here's a benchmark showing exactly this:

    enter image description here

    , , . , , - .

    , , , ( getElementsByClassName IE7 8). , , , , , , , . - 90% IE7, , , li.foo.

    +6

    .foo - . .foo, , li, ( $('ul li.doo')) , - ul.

    .foo getElementsByClassName querySelectorAll. , IE 6.

    id $('#foo'), , foo.

    +3

    All Articles