Javascript 为什么$(';.classname';)&;getElementsByClassName(';classname';)是否返回不同的内容?
为什么$('.classname')是javascript等价文档时只返回一个元素。getElementsByClassname('classname')返回一个元素数组?如果这不是彼此的等价物,那又是什么呢?如何使用jQuery获取所有具有相同类名的元素?除了$('.classname')之外,还有其他方法吗? 比如说,Javascript 为什么$(';.classname';)&;getElementsByClassName(';classname';)是否返回不同的内容?,javascript,jquery,html,Javascript,Jquery,Html,为什么$('.classname')是javascript等价文档时只返回一个元素。getElementsByClassname('classname')返回一个元素数组?如果这不是彼此的等价物,那又是什么呢?如何使用jQuery获取所有具有相同类名的元素?除了$('.classname')之外,还有其他方法吗? 比如说, <tr> <td class="currentMonth">1</td> <td class="currentMonth">
<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>
1.
2.
3.
4.
5.
如果我使用document.getElementsByClassName('currentmount'),那么我将得到上面提到的所有元素的数组
[ <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> ]
[1,2,3,4,5]
但是使用$('.currentMonth')
,我只能看到一个元素
如何使用$
获取所有元素?$('.classname')
是一个jQuery对象,而
document.getElementsByCassName('classname')
是DOM对象的列表
$('.classname')
将选择与类claaname
匹配的所有元素,并将其作为jQuery对象
$('.classname').html(“任意”)
将删除所有.classname
元素。$('.currentMonth')
返回所有匹配元素的jQuery对象。它以jQuery的方式包装,但也返回所有元素。您可以使用以下方法获取元素:
$('.currentMonth').each(function () {
this; // Here this refers to each of the matched element.
});
而document.getElementsByClassname('currentmount')
返回DOM对象的列表
例如,如果我正在执行这样的脚本:
$('.currentMonth').html("Hello!");
所有的
都将被更改
document.getElementsByClassname('classname')
使用浏览器内置方法返回一个NodeList
对象,该对象表示具有指定类名的元素集合,而$(“.className”)
返回jQuery对象,您可以使用jQuery方法对其进行操作
尝试$('.currentMonth')。删除()