[英]Why does jQuery class selector only return one element?
There are multiple divs with the class in the document. 该文档中的类有多个div。
As seen in the console, document.getElementsByClassName
produces: 从控制台中可以看到,
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>
and jquery, with the same class selector produces: 和具有相同类选择器的jquery产生:
$('.current-pad-o')
// (the first div only - no collection)
<div class="red current-pad-o"> first value </div>
I would expect a collection from the jquery statement also. 我希望也从jquery语句的集合。 These were output in both Safari and Firefox.
这些都是在Safari和Firefox中输出的。
According to the jQuery Class Selector documentation , the second selector Selects all elements with the given class . 根据jQuery Class Selector文档 ,第二个选择器选择具有给定类的所有元素 。
Why does jquery only return one, instead of a collection? 为什么jquery只返回一个而不是一个集合?
You do not have jQuery, you are using the debugger's shortcut for document.querySelector()
. 您没有jQuery,正在使用调试器的
document.querySelector()
快捷方式。 If you would use $$('.current-pad-o')
, you would get all of them. 如果使用
$$('.current-pad-o')
,则可以全部使用。
Too verify that you are not using jQuery, type the following into the command line: 要验证您没有使用jQuery,请在命令行中输入以下内容:
console.log($)
For querySelector, you are going to see this: 对于querySelector,您将看到以下内容:
function $(selector, [startNode]) { [Command Line API] }
For jQuery, you would see this: 对于jQuery,您将看到以下内容:
function (a,b){return new n.fn.init(a,b)}
Reference: console expressions 参考: 控制台表达式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.