简体   繁体   English

为什么$('。classname')和document.getElementsByClassName('classname')返回不同的东西?

[英]why does $('.classname') & document.getElementsByClassName('classname') return different things?

Why does $('.classname') return just one element when it's javascript equivalent document.getElementsByClassname('classname') returns an array of elements? 为什么$('。classname')与javascript等价的文档只返回一个元素。getElementsByClassname('classname')返回一个元素数组? If it's not each other's equivalent, what is then? 如果不是彼此相等,那又是什么? How to get all the element which have the same class name using jQuery? 如何使用jQuery获取具有相同类名的所有元素? Is there any other way other than $('.classname') ? 除了$('。classname')以外,还有其他方法吗? For example, 例如,

<tr>
 <td class="currentMonth">1</td>
 <td class="currentMonth">2</td>
 <td class="currentMonth">3</td>
 <td class="currentMonth">4</td>
 <td class="currentMonth">5</td>

If i use document.getElementsByClassName('currentMonth'), then I will get an array of all the elements mentioned above. 如果我使用document.getElementsByClassName('currentMonth'),那么我将获得上述所有元素的数组。

[ <td class="currentMonth">1</td>,    <td class="currentMonth">2</td>, <td class="currentMonth">3</td>,    <td class="currentMonth">4</td>,    <td class="currentMonth">5</td> ]

But with $('.currentMonth') , I can see only one element. 但是使用$('.currentMonth') ,我只能看到一个元素。

How can I get all the elements using $ ? 如何使用$获得所有元素?

The $('.currentMonth') returns a jQuery object of all the matching elements. $('.currentMonth')返回所有匹配元素的jQuery对象。 It is wrapped in the jQuery way, but it also returns all the elements. 它以jQuery方式包装,但它也返回所有元素。 You can get the elements by using: 您可以使用以下方法获取元素:

$('.currentMonth').each(function () {
  this; // Here this refers to each of the matched element.
});

Whereas document.getElementsByClassname('currentMonth') returns a list of DOM Objects. document.getElementsByClassname('currentMonth')返回DOM对象的列表。

So for example, if I am executing a script like this: 因此,例如,如果我正在执行这样的脚本:

$('.currentMonth').html("Hello!");

All the <td> s will be changed. 所有<td>都将被更改。

$('.classname') is a jQuery Object, whereas document.getElementsByClassname('classname') is a list of DOM Object $('.classname')是一个jQuery对象,而document.getElementsByClassname('classname')是一个DOM对象列表

$('.classname') will select all the elements which matches with the class claaname and will make a jQuery Object of it. $('.classname')将选择与类claaname匹配的所有元素,并将其制成jQuery Object。

$('.classname').html("Whatever") will remove all the .classname elements. $('.classname').html("Whatever")将删除所有.classname元素。

document.getElementsByClassname('classname') 

return a NodeList object , representing collection of elements with specified class name,using browsers built-in methods while $(".className") return jQuery object and you can use jQuery methods to manipulate it 使用浏览器的内置方法返回NodeList对象,该对象表示具有指定类名称的元素的集合,而$(".className")返回jQuery对象,您可以使用jQuery方法进行操作

Explore More 探索更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么document.getElementsByClassName(“ className”)返回对象 - why document.getElementsByClassName(“className”) returns object 使用document.getElementsByClassName切换类名-JavaScript - Toggle Classname using document.getElementsByClassName - JavaScript document.getElementsByClassName(&#39;className&#39;) 给出 undefined 当它不是 - document.getElementsByClassName('className') gives undefined when its not 如何将style.animation与document.getElementsByClassName(“ classname”)结合使用 - how to use document.getElementsByClassName(“classname”) with style.animation 为什么不能使用“ !! document.getElementsByClassName &amp;&amp; function(){return document.getElementsByClassName(obj)}”简化代码 - Why can't use “ !!document.getElementsByClassName && function(){return document.getElementsByClassName(obj)}” for simplifing codes 为什么 document.getElementsByName 返回一个 NodeList 而 document.getElementsByClassName 返回一个 HTMLCollection? - Why does document.getElementsByName return a NodeList while document.getElementsByClassName returns an HTMLCollection? 为什么在调用document.getElementsByClassName时我的函数返回未定义? - Why does my function return undefined when calling document.getElementsByClassName? 为什么返回document.getElementsByClassName可以正常工作,而getElementbyId不在Watir中呢? - Why is that return document.getElementsByClassName is working and getElementbyId is not in Watir? 为什么document.getElementsByClassname()杀死Tizen Web应用程序? - Why does document.getElementsByClassname() kill tizen web app? 为什么document.GetElementsByClassName在本地驱动器上有效,但在网络驱动器上无效? - Why does document.GetElementsByClassName work on local drive but not on a network drive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM