例如我有一个 <div class="first">
包含其他<div>
并选择第一个并将其插入变量
variable = $('div.first')
现在我想要这样的东西
$(variable + " > div")
我知道,这看起来很愚蠢,但是如果我有一个包含表行的数组并且我需要访问特定行内的列怎么办:
var tableRows = $('table > tbody > tr');
var tableBodyCols = $(tableRows[i]+'> td'); // Doesn't work :(
最佳答案
var tableBodyCols = tableRows.children('td');
或者上下文选择器:
var tableBodyCols = $('> td', tableRows);
两者是等效的,但恕我直言,前者更容易阅读。
这是一个fiddle展示两者。
<小时/>并迭代 each
的所有子集表格行:
tableRows.each(function() {
var tableBodyCols = $(this).children('td');
//...
});
但是,如果您确实更喜欢 for
循环或者需要获取特定行的列,那么 eq
是你的 friend 。
for (var i = 0; i < tableRows.length; i++) {
var tableBodyCols = tableRows.eq(i).children('td');
//...
}
这是更新后的 fiddle .
关于jquery:使用 (“parent > child” )和变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15764082/