选择tr及其“所有子元素”;-查找jquery选择器
Select tr and its "tr childs" - find jquery selector
我的表如下所示:
<table class="highlight">
<tbody>
<tr class="sortable"><td>first row</td></tr>
<tr class="sortable nondraggable sub"><td>collasped row with further informations (belongs to row one)</td></tr>
<tr class="sortable nondraggable sub"><td>collasped row with further informations (belongs to row one)</td></tr>
<tr class="sortable nondraggable sub"><td>collasped row with further informations (belongs to row one)</td></tr>
<tr class="sortable"><td>second row</td></tr>
<tr class="sortable"><td>third row</td></tr>
<tr class="sortable nondraggable sub"><td>collasped row with further informations (belongs to third row)</td></tr>
<tr class="sortable"><td>fourth row</td></tr>
<tr class="sortable"><td>fifth row</td></tr>
<tr class="sortable nondraggable sub"><td>collasped row with further informations (belongs to fifth row)</td></tr>
<tr class="sortable nondraggable sub"><td>collasped row with further informations (belongs to fifth row)</td></tr>
</tbody>
我想选择每一行及其"折叠"子行。
说明:https://jsfiddle.net/4k1a6xp6/4/
我认为可以通过使用jquery的子">"
选择器来解决。所以我们可以用jQuery("table.highlight").find("tr.sortable > ??? ")
? ?在到达下一个父行之前,应该选择带有" sortable nondraggable sub
"的子行。
我怎样才能做到这一点?
[EDIT]基于答案的一个可能的解决方案:https://jsfiddle.net/4k1a6xp6/6/
选择父行(使用如下所示的特定选择器,或者根据您的需求在每个选择器上迭代),然后使用nextUntil with:not selector。例如:
$('tr.sortable').first().nextUntil('tr.sortable:not(.nondraggable.sub)')
OK,据我所知,您想同时选择父行和折叠行。
看看下面的小提琴
$('tr.sub').css('background-color', 'yellow');
var sibling = $('tr.sub').prev();
//console.log(sibling);
$(sibling).each(function(index, item) {
console.log(item);
if (!$(item).hasClass('sub')) {
$(item).css('background-color', 'blue');
}
});
它假设您的"父"行没有任何类,并且检查父行缺失的.sub类。
颜色不同只是为了突出显示。如果您可以将类添加到父行,这将更容易:)
代码段
$('tr.sub').css('background-color', 'yellow');
var sibling = $('tr.sub').prev();
//console.log(sibling);
var solorows = $('tr.sortable').next('.sortable:not(.nondraggable.sub)');
$(solorows).each(function(index,item){
//console.log(item);
if($(item).next('tr.nondraggable.sub')){
$(item).css('background-color','red')
}
});
$(sibling).each(function(index, item) {
// console.log(item);
if (!$(item).hasClass('sub')) {
$(item).css('background-color', 'yellow');
}
});
.highlight {
table-layout: fixed;
border-collapse: collapse;
}
td {
border: 1px solid black;
padding: 2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="highlight">
<tbody>
<tr class="sortable">
<td>first row</td>
</tr>
<tr class="sortable nondraggable sub">
<td>collasped row with further informations (belongs to row one)</td>
</tr>
<tr class="sortable nondraggable sub">
<td>collasped row with further informations (belongs to row one)</td>
</tr>
<tr class="sortable nondraggable sub">
<td>collasped row with further informations (belongs to row one)</td>
</tr>
<tr class="sortable">
<td>second row</td>
</tr>
<tr class="sortable">
<td>third row</td>
</tr>
<tr class="sortable nondraggable sub">
<td>collasped row with further informations (belongs to third row)</td>
</tr>
<tr class="sortable">
<td>fourth row</td>
</tr>
<tr class="sortable">
<td>fifth row</td>
</tr>
<tr class="sortable nondraggable sub">
<td>collasped row with further informations (belongs to fifth row)</td>
</tr>
<tr class="sortable nondraggable sub">
<td>collasped row with further informations (belongs to fifth row)</td>
</tr>
</tbody>
相关文章:
- JavaScript选择器:查找包含具有特定属性的子体的节点
- 如何查找包含特定选择器的父元素
- jQuery选择器,用于查找包含具有特定值的TD输入的TR
- 如何使用 jQuery 筛选器选择器查找表标记 td 值
- 如何查找不比选择器更深的元素
- CasperJS可以't使用CSS选择器或xPath查找元素
- 与其在 jquery 中选择器中查找元素,我可以反过来做
- 查找带有多个选择器的 prev() 标签
- jQuery根据类选择器匹配父级查找子项
- jQuery选择器,用于查找与选择器匹配的给定元素之后的第一个元素
- jQuery 查找不适用于属性选择器
- 使用查询选择器查找同级行中包含的下一个单元格
- 查找选择器的第一个父级
- 如何基于两个选择器查找元素
- Jquery 选择器来查找所有没有特定类的输入
- 是否有一个 jQuery 选择器查找不存在的子字符串
- 在数组中查找选择器
- 替换jquery中的多个查找选择器
- Mongo查找选择器返回不匹配的结果
- 使用find查找选择器的子级