我创建一个表格图1,我想要设置鼠标按住摘要和科目中间的边框来回拖动调节这两个单元格的大小,当拖到摘要单元格小于120px就停止,我也在摘要单元格设置了最小宽度值,但是不管用图2,希望学长指点一下


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2</title>
<style>
.divtab {
width: 900px;
margin-left: 80px;
margin-top: 80px;
}
table{
width: 100%;
table-layout: fixed;
overflow: hidden;
white-space:nowrap;
text-overflow: ellipsis;
}
.xhtd{
width: 80px;
height: 68px;
}
.zytd{
height: 38px;
min-width: 130px;
}
.kmtd{
height: 38px;
min-width: 130px;
}
.jdtd{
width: 208px;
}
.style1{
height: 60px;
}
</style>
</head>
<body>
<div class="divtab">
<table id="tb_1" cellspacing="0" width="100%" border="1" >
<tbody>
<tr>
<td class="xhtd" rowspan="2">序号</td>
<td class="zytd" rowspan="2">摘要</td>
<td class="kmtd" rowspan="2">科目</td>
<td class="jdtd wd4">借方金额</td>
<td class="jdtd wd5">贷方金额</td>
</tr>
<tr>
<td class="style">123</td>
<td class="style">456</td>
</tr>
<tr>
<td class="style1" wd></td>
<td class="style1"></td>
<td class="style1"></td>
<td class="style1" wd4></td>
<td class="style1" wd5></td>
</tr>
<tr>
<td class="style1" wd></td>
<td class="style1"></td>
<td class="style1"></td>
<td class="style1" wd4></td>
<td class="style1" wd5></td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript">
function dragclle(){
var tTD; //用来存储当前更改宽度的Table Cell,避免快速移动鼠标的问题
var table = document.getElementById("tb_1");
// for (j = 1; j < table.rows[0].cells.length - 3; j++) {
// table.rows[0].cells[j].onmousedown = function () {
rows= table.getElementsByTagName('tr')
console.log(rows);
for (i = 0; i < 1; i++) {
for (j = 1; j < rows[i].cells.length - 3; j++) {
rows[i].cells[j].onmousedown = function () {
//记录单元格
tTD = this;
if (event.offsetX > tTD.offsetWidth - 10) {
tTD.mouseDown = true;
tTD.oldX = event.x;
tTD.oldWidth = tTD.offsetWidth;
}
//记录Table宽度
//table = tTD; while (table.tagName != ‘TABLE') table = table.parentElement;
//tTD.tableWidth = table.offsetWidth;
};
rows[i].cells[j].onmouseup = function () {
//结束宽度调整
if (tTD == undefined) tTD = this;
tTD.mouseDown = false;
tTD.style.cursor = 'default';
};
rows[i].cells[j].onmousemove = function () {
//更改鼠标样式
if (event.offsetX > this.offsetWidth - 10){
this.style.cursor = 'col-resize';
}else {
this.style.cursor = 'default';
}
//取出暂存的Table Cell
if (tTD == undefined) tTD = this;
//调整宽度
if (tTD.mouseDown != null && tTD.mouseDown == true) {
tTD.style.cursor = 'default';
if (tTD.oldWidth + (event.x - tTD.oldX)>0)
tTD.width = tTD.oldWidth + (event.x - tTD.oldX);
//调整列宽
tTD.style.width = tTD.width;
tTD.style.cursor = 'col-resize';
//调整该列中的每个Cell
table = tTD; while (table.tagName != 'TABLE') table = table.parentElement;
for (j = 0; j < table.rows.length; j++) {
table.rows[j].cells[tTD.cellIndex].width = tTD.width;
}
//调整整个表
// table.width = tTD.tableWidth + (tTD.offsetWidth – tTD.oldWidth);
// table.style.width = table.width;
}
};
}
}
}
dragclle()
</script>
</body>
</html>