jQuery:循环遍历两个ID内的所有元素

发布于 2024-12-10 05:53:25 字数 209 浏览 0 评论 0原文

我试图循环遍历两个 div 内的每个 .required:#A 和 #B。

$('#A .required').nextUntil('#B').each(function() {
    console.log($(this).attr("name"));
});

上面似乎只获取了两个 div 中的最后一个元素。我该如何解决这个问题?

I'm trying to loop through each .required within two divs: #A and #B.

$('#A .required').nextUntil('#B').each(function() {
    console.log($(this).attr("name"));
});

The above only seems to get the last element within the two divs. How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怼怹恏 2024-12-17 05:53:25

不太确定我理解这背后的 HTML,但也许您想要:

$('#A .required, #B .required').each(function() { ... });

这会获取所有标记为“required”类的元素,这些元素是 id 为“A”的元素或 id 为“B”的元素的后代。

Not exactly sure I understand the HTML behind this, but maybe you want:

$('#A .required, #B .required').each(function() { ... });

That gets all the elements marked with class "required" that are descendants of either the element with id "A" or the one with id "B".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文