jQuery:如何使用特殊类来寻址子项

发布于 2024-12-04 03:04:09 字数 1140 浏览 1 评论 0原文

我想迭代 line 元素。每个 line 元素都有 item 元素作为子元素。因此,首先我迭代 line,然后我想处理某个 item 元素,例如第三个。这是我的代码:

$(".pricetable .righttable .line:not(.blank)").each(function(j) {
    var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);
    currentLine.(".item").each(function(k) {
        $(".pricetable .righttable .line .item").eq(i).addClass("active");
    });
});

问题是我不知道如何使用 item 类来处理子元素。

currentLine.(".item").each(function(k)

这是行不通的。

XML 过滤器应用于非 XML 值 ({0:#2=({}), length:1, prevObject:{length:3, prevObject:{0:#1=({}), context:#1# , 长度:1}, 上下文:#1#, 选择器:".pricetable .righttable .line:not(.blank)", 0:#2#, 1:({}), 2:({})},上下文:#1#,选择器:“.pricetable .righttable .line:not(.blank).slice(0,1)”}) 文件:///C:/Users/xxx/lib/pricetable.js 第 112 行

编辑:
哇,我没想到我得到了如此好的和快速的回​​应!我想我会采用这个解决方案:

$(".pricetable .righttable .line:not(.blank)").each(function(j) {
    $(this).children(".item").eq(i).addClass("active");
});

I want to iterate over the line elements. Each line element has item elements as child elements. So first I iterate over line and then I want to address a certain item element e.g. the third. This is the code I have:

$(".pricetable .righttable .line:not(.blank)").each(function(j) {
    var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);
    currentLine.(".item").each(function(k) {
        $(".pricetable .righttable .line .item").eq(i).addClass("active");
    });
});

The problem is that I don't know how to address the child element with the item class.

currentLine.(".item").each(function(k)

This does not work.

XML filter is applied to non-XML value ({0:#2=({}), length:1, prevObject:{length:3, prevObject:{0:#1=({}), context:#1#, length:1}, context:#1#, selector:".pricetable .righttable .line:not(.blank)", 0:#2#, 1:({}), 2:({})}, context:#1#, selector:".pricetable .righttable .line:not(.blank).slice(0,1)"})
file:///C:/Users/xxx/lib/pricetable.js
Line 112

Edit:
Wow, I didn't thought I get such a good and fast response! I think I go with this solution:

$(".pricetable .righttable .line:not(.blank)").each(function(j) {
    $(this).children(".item").eq(i).addClass("active");
});

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

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

发布评论

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

评论(5

白龙吟 2024-12-11 03:04:09
$(".pricetable .righttable .line:not(.blank)").each(function(j) {
    var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);
    currentLine.find(".item").each(function(k) {
        $(".pricetable .righttable .line .item").eq(i).addClass("active");
    });
});

http://api.jquery.com/find/

如果它们是 currentLine 的直接子级,则应该能够用 children('.item') 代替 find('.item')。这称为遍历 DOM。访问此页面 (http://api.jquery.com/category/traversing/) 并阅读其中一些函数的描述。如果您经常穿越,记住它们将非常有用:)

$(".pricetable .righttable .line:not(.blank)").each(function(j) {
    var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);
    currentLine.find(".item").each(function(k) {
        $(".pricetable .righttable .line .item").eq(i).addClass("active");
    });
});

http://api.jquery.com/find/

If they're direct children of currentLine, you should just be able to do children('.item') in place of find('.item'). This is called Traversing the DOM. Visit this page (http://api.jquery.com/category/traversing/) and read through the descriptions of some of the functions there. They'll be very useful to keep in mind if you traverse a lot :)

初见你 2024-12-11 03:04:09
$('.pricetable .righttable .line:not(.blank)').find('.item').each(function() {
  // this point to child element - item
  // use $(this).addClass('active');
});
$('.pricetable .righttable .line:not(.blank)').find('.item').each(function() {
  // this point to child element - item
  // use $(this).addClass('active');
});
梦幻之岛 2024-12-11 03:04:09

在我回答您的问题之前,略有改进:

var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);

可以轻松替换为:

var currentLine = $(this);

其次,如果您希望在 $(this) 中找到第三个 .item 元素,请使用 .find()

.find(".item:nth-child(3)");

无需遍历各行。如果您必须迭代这些行,那么只需将 k2 进行比较(JavaScript 从 0 开始计数)即可。

Slight improvement before I answer your question:

var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);

is easily replaceable with:

var currentLine = $(this);

Second, if you wish to find the 3rd .item element inside of $(this), use .find()

.find(".item:nth-child(3)");

No need to iterate over the lines. If you must iterate over the lines, then just compare k to 2 (As JavaScript counts from 0) and that's it.

那些过往 2024-12-11 03:04:09

如果您想对每行的第n个item做某事,它比看起来容易得多:

var index = 3; // the third "item" element 
$(".pricetable .righttable .line:not(.blank) .item:nth-child(" + index + ")")
  .addClass("active");

查看实际效果

您是否还有其他想做的事情,但上述内容还不够?

If you want to do something with the nth item of each line, it's much easier than it looks:

var index = 3; // the third "item" element 
$(".pricetable .righttable .line:not(.blank) .item:nth-child(" + index + ")")
  .addClass("active");

See it in action.

Is there something else you want to do that the above is not sufficient for?

夏尔 2024-12-11 03:04:09
$(".pricetable .righttable .line:not(.blank)").each(function() {
  $(this).find('.item:nth-child(2)').addClass('active');
});
$(".pricetable .righttable .line:not(.blank)").each(function() {
  $(this).find('.item:nth-child(2)').addClass('active');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文