【问题标题】:What's the difference between these .find() usages?这些 .find() 用法之间有什么区别?
【发布时间】:2013-12-19 00:59:27
【问题描述】:

有什么区别

$data.find('select:selected');

$data.find('select').find(':selected');

?

假设$data = $('#data');

  <div id="data">
    <div>
    <select multiple>
    <option value="foo">foo</option>
    </select>
    </div>
    </div>

【问题讨论】:

    标签: jquery jquery-selectors find


    【解决方案1】:
    $data.find('select').find(':selected');
    

    基本一样

    $data.find('select :selected');
    

    注意descendant selectorselect:selected 之间的空格)。这会找到在select 元素 的任何:selected 元素。

    另一方面,您的第一个选择器会找到 select:selected,这不起作用,因为 :selected applies to option elements only

    【讨论】:

      【解决方案2】:

      我猜第一个解析所有的 DOM 一次并得到你的选择器。

      第二个解析 dom 得到你的选择器并解析检索到的 dom 对象找到第二个选择器

      结果相同,但它们之间的性能可能会发生变化?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-01
        • 2011-04-10
        相关资源
        最近更新 更多