H5 写一个3x3表格,合并1和2单元格,4和7单元格

本文展示了如何使用HTML5创建一个3x3的表格,并详细说明了如何通过`colspan`和`rowspan`属性来合并第一行的1和2单元格,以及第四行的4和7单元格,实现自定义的表格布局。
摘要由CSDN通过智能技术生成

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <table border="1" bordercolor="red" width="200" height="200" align="center" bgcolor="aqua"> 
                    
            <tr align="center">
                <td colspan="2">1&2</td>
                
                <td>3</td>
            </tr>
            <tr align="center">
                <td rowspan="2">4</td>
                <td>5</td>
                <td>6</td>
            </tr>
            
            <tr align="center">         
             

实现框选合并单元格的过程如下: 1. 使用JavaScript获取框选的单元格范围。 2. 遍历框选的单元格范围,将其合并一个单元格。可以使用colspan和rowspan属性来实现单元格合并。 以下是一个简单的示例代码: ```html <table> <tr> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> </tr> </table> <script> var table = document.querySelector('table'); var cells = table.querySelectorAll('td'); var startCell = null; var endCell = null; table.addEventListener('mousedown', function(e) { startCell = e.target; }); table.addEventListener('mouseup', function(e) { endCell = e.target; // 获取框选的单元格范围 var startRowIndex = startCell.parentNode.rowIndex; var endRowIndex = endCell.parentNode.rowIndex; var startColIndex = startCell.cellIndex; var endColIndex = endCell.cellIndex; if (startRowIndex > endRowIndex) { var temp = startRowIndex; startRowIndex = endRowIndex; endRowIndex = temp; } if (startColIndex > endColIndex) { var temp = startColIndex; startColIndex = endColIndex; endColIndex = temp; } // 合并单元格 var colspan = endColIndex - startColIndex + 1; var rowspan = endRowIndex - startRowIndex + 1; startCell.setAttribute('colspan', colspan); startCell.setAttribute('rowspan', rowspan); // 清空其他单元格 for (var i = 0; i < cells.length; i++) { var cell = cells[i]; if (cell != startCell) { var rowIndex = cell.parentNode.rowIndex; var colIndex = cell.cellIndex; if (rowIndex >= startRowIndex && rowIndex <= endRowIndex && colIndex >= startColIndex && colIndex <= endColIndex) { cell.parentNode.removeChild(cell); } } } startCell = null; endCell = null; }); </script> ``` 在上面的代码中,我们使用了mousedown和mouseup事件来获取框选的单元格范围,并使用colspan和rowspan属性来合并单元格。注意,我们还需要清空其他单元格,以避免出现重叠的单元格
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值