在以下代码中,如何使用jquery将列2
和4
合并到具有columnspan=2
的单个列中?
<table id="Sample" border="1">
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>2</td>
</tr>
</tbody>
</table>
答案 0 :(得分:0)
请试试这个
$(document).ready(function () {
var row = $("#Sample tr td");
for (var i = 0; i < row.length; i++) {
var num1 = parseInt(row[i].innerHTML);
var num2 = parseInt(row[i + 2].innerHTML);
if (num1 == num2) {
row[i].setAttribute("colspan", "2");
row[i + 2].parentElement.removeChild(row[i + 2])
}
}
});