简体   繁体   English

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

[英]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.

相关问题 为什么 jQuery select 在选择器中 chaining.attr() 时只有一个元素? - Why does jQuery select only one element when chaining .attr() in selector? 为什么jQuery选择器返回一个数组而不是元素本身? - why does a jquery selector return an array instead of the element itself? 为什么 jQuery element[] 选择器在这种情况下不起作用? - Why does the jQuery element[] selector not work in this case? 为什么当指定选择器时,jQuery .index()方法仅适用于集合中的第一个元素? - Why does the jQuery .index() method only work for the first element in the collection, when a selector is specified? jQuery类选择器仅在类的第一个元素上注册事件 - jquery class selector only registering events on first element of class 为什么使用选择器JQuery show()函数仅对一个(而不是全部)元素起作用? - Why does JQuery show() function only work on one (rather than all) of elements with the selector? 为什么 jQuery 选择器只选择特定网站上的第一个层次结构元素? - why is jQuery selector only selecting first hierarchy element on specific website? 为什么此事件仅绑定到与选择器匹配的一个元素? - Why is this event only binding to one element matching the selector? 为什么 jQuery.find() 只返回第一个元素? - why jQuery .find() only return first element? 带有 id AND 类的 jQuery 选择器目标元素不起作用 - jQuery selector targeting element with id AND class does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM