代码之家  ›  专栏  ›  技术社区  ›  David Prem Acharya

为什么jQuery类选择器只返回一个元素?

  •  3
  • David Prem Acharya  · 技术社区  · 7 年前

    document.getElementsByClassName 生产:

    document.getElementsByClassName('current-pad-o')
    HTMLCollection (2) = $8
    0 <div class="red current-pad-o"> first value </div>
    1 <div class="red current-pad-o"> second value </div>
    

    $('.current-pad-o')
    // (the first div only - no collection)
    <div class="red current-pad-o"> first value </div>
    

    我还希望从jquery语句中得到一个集合。这些都是在Safari和Firefox中输出的。

    根据 jQuery Class Selector documentation ,第二个选择器 .

    1 回复  |  直到 7 年前
        1
  •  11
  •   epascarello    7 年前

    您没有jQuery,您正在使用调试器的快捷方式 document.querySelector() $$('.current-pad-o') ,你会得到所有的。

    还要确认您没有使用jQuery,请在命令行中键入以下内容:

    console.log($)
    

    function $(selector, [startNode]) { [Command Line API] }
    

    function (a,b){return new n.fn.init(a,b)}
    

    参考: console expressions