- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么 $('.classname') 在 javascript 等效 document.getElementsByClassname('classname') 返回一组元素时只返回一个元素?如果这不是彼此等价的话,那什么才是呢?如何使用 jQuery 获取所有具有相同类名的元素?除了 $('.classname') 之外还有其他方法吗?例如,
<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>
如果我使用 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> ]
但是使用 $('.currentMonth')
,我只能看到一个元素。
如何使用 $
获取所有元素?
最佳答案
$('.currentMonth')
返回所有匹配元素的 jQuery 对象。它以 jQuery 方式包装,但它也返回所有元素。您可以使用以下方式获取元素:
$('.currentMonth').each(function () {
this; // Here this refers to each of the matched element.
});
而document.getElementsByClassname('currentMonth')
返回 DOM 对象的列表。
例如,如果我执行这样的脚本:
$('.currentMonth').html("Hello!");
所有<td>
s 将被更改。
关于javascript - 为什么 $ ('.classname' ) & document.getElementsByClassName ('classname' ) 返回不同的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34331838/
我的意思是: body.class1 div.class1 { color: blue; } div.class1.class2 { color: red; } Hello
编译的时候一直报这个错,不知道为什么。我看过this发布和this一个,但它们似乎是不同的问题。 相关代码结构为: // main.cpp #include "MyClass.h" int main(
这个问题已经有答案了: What is a raw type and why shouldn't we use it? (16 个回答) 已关闭 9 年前。 标题非常不言自明。请问原始类型,例如Arr
我想知道在哪里记录了这种形式的构造函数调用。这种语法显然适用于 Visual Studio 6.0 版(我知道它不能使用 G++ 编译)。 请注意,我不是在寻找替代品,也不需要知道它是好是坏。 cla
这是一个菜鸟问题,抱歉,我来自 Java,不知道为什么我的 OO 东西不起作用。我有这个主要的: #include #include "Foo.h" //changed name using nam
我正在使用 Code::Blocks 构建我的项目,其中包含三个文件:main.cpp、TimeSeries.cpp、TimeSeries.h。 TimeSeries.h 为 TimeSeries 类
我想... 扫描文档以查找具有特定类名的所有元素 对该元素的 innerHTML 执行一些关键功能 更改该元素的类名,这样如果我稍后进行另一次扫描,我就不会重做该元素 我认为这段代码可以工作,但出于某
我有一些 JQuery 代码,可将特定类的所有 HTML 元素转换为文本区域元素。 我的问题:我使用 JQuery(.addClass()) 将元素类从“可更新”更改为“可更新 P”。但是当我去搜索所
为什么按属性选择类 [class="classname"] 代替按类选择 .classname ?在考虑跨电子邮件客户端兼容的电子邮件时,这样做有什么好处吗? 编辑:我完全了解每个选择器是如何工作的,
所以基本上我使用 MyLocationOverlay 在 map 上绘制用户位置。然而,我不喜欢它的绘制方式。我想要拥有自己的图标并删除该位置周围的圆圈。 我知道如何执行此操作的唯一方法是创建一个扩展
根据另一个类中给定的输入,此类必须从要在其中找到该变量的 4 个类之一检索变量。例如。如果输入是 s,则此类要查找的变量可在 Sumup.counter 中找到,而当输入是 a 时,它需要的变量可以在
从命令行运行基本的 java 程序需要 3 个步骤: 编写代码: 公共(public)类HelloWorld {公共(public)静态无效主(字符串[] args){ System.out.prin
例如 - 我只有下一行代码,如果 span 包含从 outcome 开始的某些 class ,我需要重新识别--事件。如果是这样 - 通过 JS 删除它。 我们有什么想法可以实现它吗? 我接下来尝试
我正在学习 Java,所以请多多包涵,即使这对您来说很简单。 我正在使用两个链表: private LinkedList x; private LinkedList y; public List()
相关:Jquery timeout for doubleclick 鉴于此代码: function handleClick() { var div = $(this); var origina
#ifndef RESERVATIONS_H_INCLUDED #define RESERVATIONS_H_INCLUDED #include #include class Reservatio
我在我正在处理的代码库中看到一些代码如下: ZfooName::ZfooName(int magoo) : ZfooName() { fGoo = magoo; } 我假设这是一
我要找 IReadOnlyCollection 使用 WebDriverWait 确保元素已在页面上呈现。 这是我的代码 WebDriverWait wait = new WebDriverWait
我有这两种方法来编写构造函数。 className()和 className._() 它们之间有什么区别,我什么时候应该使用哪个? class GlobalState{ final Ma
下面的方法 公共(public) JSound() { } 从来没有人要求提供此代码?有什么原因吗? 如果我不能让它以这种方式工作,还有第二种方法可以使用鼠标监听器吗?它需要一个非静态方法,所以我不确
我是一名优秀的程序员,十分优秀!