日期:2014-05-16  浏览次数:20292 次

求助,如何使用js删除行,效果如下图所示

------解决方案--------------------
先拆分再合并的思路,可能复杂了点。


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(function(){
        $("#div1 table input[type=button]").click(function(){
           var needRemove = $(this).parents("tr"),
tab = $(this).parents("table"),
trs = tab.find("tr"),
cache={}
;
var index=0;

//拆分
trs.each(function(col){
$(this).find("td").each(function(row){
var iRowSpan = $(this).attr("rowspan") 
------解决方案--------------------
 0;
if(iRowSpan>1){
cache[index]={rowspanIndex:index,html:$(this).html()};
$(this).attr("coldata", index).removeAttr("rowspan");

for(var i=1;i<iRowSpan;i++ ){
var ttr= trs[col+ i],
toAdd = $(ttr).find("td")[row-1];
if(toAdd){
$(toAdd).after($("<td>").attr("coldata", index));
}
}
index++;
}
});
});
//删除
$(needRemove).remove();

//重新合并
trs = tab.find("tr");
trs.each(function(col){
$(this).find("td").each(function(row){
var colIndex=$(this).attr("coldata"),  data= cache[colIndex];
if(!data) return;
if(!data.tag) {
data.tag= this;
$(this).html(data.html).removeAttr("coldata");
return;
}
data.tag.rowSpan ++;
$(this).remove();
});
});
        });
    })
</script>
<div id="div1"> 
<table border="1">
 
        <tr>
                <td>1</td>
                <td rowspan="5">2000+5000</td>
                <td rowspan="5">24000</td>
                <td>1000</td>
                <td>2000</td>
                <td>3000</td>
                <td><input type="button" value="删除行"/></td>
        </tr>
        <tr>
                <td>2</td>
                <td>1000</td>
   &nbs