jQuery:如何使用特殊类来寻址子项
我想迭代 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
http://api.jquery.com/find/
如果它们是 currentLine 的直接子级,则应该能够用
children('.item')
代替find('.item')
。这称为遍历 DOM。访问此页面 (http://api.jquery.com/category/traversing/) 并阅读其中一些函数的描述。如果您经常穿越,记住它们将非常有用:)http://api.jquery.com/find/
If they're direct children of currentLine, you should just be able to do
children('.item')
in place offind('.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 :)在我回答您的问题之前,略有改进:
可以轻松替换为:
其次,如果您希望在
$(this)
中找到第三个.item
元素,请使用.find()
无需遍历各行。如果您必须迭代这些行,那么只需将
k
与2
进行比较(JavaScript 从 0 开始计数)即可。Slight improvement before I answer your question:
is easily replaceable with:
Second, if you wish to find the 3rd
.item
element inside of$(this)
, use.find()
No need to iterate over the lines. If you must iterate over the lines, then just compare
k
to2
(As JavaScript counts from 0) and that's it.如果您想对每行
行
的第n个item
做某事,它比看起来容易得多:查看实际效果。
您是否还有其他想做的事情,但上述内容还不够?
If you want to do something with the nth
item
of eachline
, it's much easier than it looks:See it in action.
Is there something else you want to do that the above is not sufficient for?