我有一些桌子结构:
<tr class="row-2"><tr>
<tr class="row-3">..<tr>
<tr class="row-4">..<tr>
<tr class="row-5">..<tr>
<tr class="row-6">..<tr>
<tr class="row-7"><tr>
<tr class="row-8">..<tr>
<tr class="row-9">..<tr>
<tr class="row-10">..<tr>
<tr class="row-11">..<tr>
...etc对于这个例子,TR与类“行-2”和“行-7”是parrent产品链接,其中包括展开子行。
<script>
$(function() {
$('tr.parent')
.css("cursor","pointer")
.css("color","red")
.attr("title","Click to expand/collapse")
.click(function(){
$(this).siblings('.child-'+this.id).toggle();
});
$('tr[@class^=child-]').hide().children('td');
});
</script>行-3.-6是第2行的子行,和行-8.-11是第7行的子行-7
如何找到第2行、第7行等,然后添加第二类“父类”和ID类似类(id=“行-2”、id=“行-7”等)?另外,我需要在每个TR之间添加行-2和行-7类,与前面的父行相同。说到底,我需要这样的东西:
<tr class="row-2 parent" id="row-2"><tr>
<tr class="row-3 child-row2">..<tr>
<tr class="row-4 child-row2">..<tr>
<tr class="row-5 child-row2">..<tr>
<tr class="row-6 child-row2">..<tr>
<tr class="row-7 parent" id="row-7"><tr>
<tr class="row-8 child-row7">..<tr>
<tr class="row-9 child-row7">..<tr>
<tr class="row-10 child-row7">..<tr>
<tr class="row-11 child-row7">..<tr>
..etc发布于 2010-03-29 05:05:05
一种方法是对所有行进行两次传递,设置所需的类和id。在第一次传递中,添加一个与类名相同的id,并将parent类添加到第2行和第7行。在第二次传递中,查找每个.parent行,并将child-<id>类添加到他们的兄弟姐妹,直到下一个父类。这不是对第2行和第7行的值进行硬编码,而是假定所有头项都包含在<th>元素中,而不是<td>元素中。
下面是一些代码:http://jsfiddle.net/vYTW2/
/**
* Do a two pass on all rows to set them up.
* In the first pass, add the same id as class, and
* add the "parent" class to the row.
*
* Assuming all product header rows contain a
* <th> element
*/
$('table tr th').each(function() {
var row = $(this).parent('tr');
var id = row.attr('class');
row.attr('id', id);
row.addClass('parent');
});
/**
* In the second pass, run through all header rows
* (that have the class parent) and mark all next siblings
* with class "child-rowX" until we see the next parent row.
*/
$('tr.parent').each(function() {
var id = $(this).attr('id');
var className = 'child-' + id;
$(this).nextUntil('.parent').addClass(className);
});https://stackoverflow.com/questions/2535767
复制相似问题