如何用jQuery修改属性ROWSPAN?

时间:2012-03-13 01:14:41

标签: javascript jquery css html-table

<table border="1">
    <tr><td>111</td><td>22</td><td rowspan="3">ads</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
    <tr><td>111</td><td>22</td><td rowspan="3">ads</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
    <tr><td>111</td><td>22</td><td rowspan="3">ads</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
    <tr><td>111</td><td class="remove">22</td></tr>
</table>

$('.remove').click(function(){
   $(this).parent().remove();
})

FIDDLE:http://jsfiddle.net/r5BDW/1/

如果我删除TR,那么表就会中断,因为ROWSPAN太大了。可以修改ROWSPAN吗?如果是,怎么样?

3 个答案:

答案 0 :(得分:7)

 $('.remove').click(function(){
     $(this).parent()
            .prevAll('tr:has(td[rowspan]):first')
            .find('td[rowspan]')
            .attr('rowspan', function(i, rs) { return rs - 1; })
            .end()
            .end()
            .remove();
 });

答案 1 :(得分:4)

我希望attr()方法有效...试试这个:

$('selector for the element you want to modify').attr('rowspan', 'newvalue');

例如:

$('#myCell').attr('rowspan', '2');

答案 2 :(得分:1)

万一你需要它,我已经编写了一个功能,可以自动检测列中重复的名称并添加rowspan。您只需要提供一列(td值数组)。

&#13;
&#13;
var column1 = $('.modified_table td:first-child');
var column2 = $('.modified_table td:nth-child(2)');
var column3 = $('.modified_table td:nth-child(3)');

modifyTableRowspan(column1);
modifyTableRowspan(column2);
modifyTableRowspan(column3);

function modifyTableRowspan(column) {

        var prevText = "";
        var counter = 0;

        column.each(function (index) {


            var textValue = $(this).text();

            if (index === 0) {
                prevText = textValue; 
            }
            
            if (textValue !== prevText || index === column.length - 1) {

                var first = index - counter;

                if (index === column.length - 1) {
                    counter = counter + 1;
                }

                column.eq(first).attr('rowspan', counter);


                if (index === column.length - 1)
                {
                    for (var j = index; j > first; j--) {
                        column.eq(j).remove();
                    }
                }

                else {

                    for (var i = index - 1; i > first; i--) {
                        column.eq(i).remove();
                    }
                }

                prevText = textValue;
                counter = 0;
            }

            counter++;

        });

    }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">

</script>

Before:
<table class="unmodified_table" border="1">
    <tr><td>111</td><td>22</td><td>ads</td></tr>
    <tr><td>111</td><td >22</td><td>ads</td></tr>
    <tr><td>111</td><td >22</td><td>abs</td></tr>
    <tr><td>111</td><td>22</td><td>ads</td></tr>
    <tr><td>111</td><td >22</td><td>abs</td></tr>
    <tr><td>111</td><td >22</td><td>ads</td></tr>
    <tr><td>111</td><td>22</td><td>ads</td></tr>
    <tr><td>111</td><td >22</td><td>ads</td></tr>
    <tr><td>111</td><td >22</td><td>abs</td></tr>
</table>
<br /> </br>

After:
<table class="modified_table" border="1">
    <tr><td>111</td><td>22</td><td>ads</td></tr>
    <tr><td>111</td><td >22</td><td>ads</td></tr>
    <tr><td>111</td><td >22</td><td>abs</td></tr>
    <tr><td>111</td><td>22</td><td>ads</td></tr>
    <tr><td>111</td><td >22</td><td>abs</td></tr>
    <tr><td>111</td><td >22</td><td>ads</td></tr>
    <tr><td>111</td><td>22</td><td>ads</td></tr>
    <tr><td>111</td><td >22</td><td>ads</td></tr>
    <tr><td>111</td><td >22</td><td>abs</td></tr>
</table>
&#13;
&#13;
&#13;

JSFiddle:https://jsfiddle.net/1ewk227x/2/