admin管理员组文章数量:1304954
There are multiple divs with the class in the document.
As seen in the console, document.getElementsByClassName
produces:
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:
$('.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. These were output in both Safari and Firefox.
According to the jQuery Class Selector documentation, the second selector Selects all elements with the given class.
Why does jquery only return one, instead of a collection?
There are multiple divs with the class in the document.
As seen in the console, document.getElementsByClassName
produces:
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:
$('.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. These were output in both Safari and Firefox.
According to the jQuery Class Selector documentation, the second selector Selects all elements with the given class.
Why does jquery only return one, instead of a collection?
Share Improve this question edited Jun 26, 2017 at 23:12 David asked Jun 26, 2017 at 23:10 DavidDavid 3,3231 gold badge41 silver badges57 bronze badges 5-
seems like the
document.getElementsByClassName
is returning the wrong thing!! That second div doesn NOT include the classcurrent-pad-o
– Jaromanda X Commented Jun 26, 2017 at 23:12 - I am willing to bet it ain't jQuery, I am willing to bet what you think is jQuery is the debugger's shortcut for document.querySelector – epascarello Commented Jun 26, 2017 at 23:12
-
But that's not the same class selector - one is
current-pad-o
and the other one iscurrtrip-pad-o
. They are different. – Wayne Allen Commented Jun 26, 2017 at 23:12 - Question was edited. The jQuery selector works with the updated example. – Alexander Higgins Commented Jun 26, 2017 at 23:15
- Please provide a minimal reproducible example that reproduces issue – charlietfl Commented Jun 26, 2017 at 23:17
1 Answer
Reset to default 12You do not have jQuery, you are using the debugger's shortcut for document.querySelector()
. If you would use $$('.current-pad-o')
, you would get all of them.
Too verify that you are not using jQuery, type the following into the mand line:
console.log($)
For querySelector, you are going to see this:
function $(selector, [startNode]) { [Command Line API] }
For jQuery, you would see this:
function (a,b){return new n.fn.init(a,b)}
Reference: console expressions
本文标签: javascriptWhy does jQuery class selector only return one elementStack Overflow
版权声明:本文标题:javascript - Why does jQuery class selector only return one element? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741792585a2397747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论