<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>鉴本</title> <style> table { border-collapse: collapse; } td, th { border: 1.5px solid black; padding: 5px; } table img { max-width: 150px; height: auto; } <!-- //图片大小调整 --> .popup { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.8); display: flex; align-items: center; justify-content: center; } .popup img { max-width: 100%; max-height: 100%; cursor: zoom-out; } <!-- //放大图片设置 --> </style> </head> <body> <div> <button onclick="addRow()">新增一行</button> <button onclick="addColumn()">添加一列</button> <button onclick="deleteRow()">删除行</button> <button onclick="deleteColumn()">删除列</button> <button onclick="changeSize()">调整大小</button> <div> <div id="popup" class="popup" style="display: none;"> <img id="popupImg"> </div> <table id="myTable"> <thead> <colgroup> <col style="width: 40px !important;"> </colgroup> <tr>var table = document.getElementById("myTable"); var imageColumnIndices = [8, 9, 10,11,12,13]; // 指定要添加图片按钮的列索引数组 function addRow() { var newRow = table.insertRow(); var cellsCount = table.rows[0].cells.length; for (var i = 0; i < cellsCount; i++) { var newCell = newRow.insertCell(); newCell.contentEditable = true; if (imageColumnIndices.includes(i)) { var newInput = document.createElement("input"); newInput.type = "file"; newInput.onchange = function() { previewImage(this); }; newInput.onclick = function() { showPopup(this.src); }; newCell.appendChild(newInput); // 点击事件监听器,点击图片按钮时弹出放大图片的弹出窗口 newInput.onclick = function () { showPopup(this.src); }; }else { newCell.innerHTML = "新单元格"; // 将空余按钮的单元格内容设置为默认值 } } } function addColumn() { var cellsCount = table.rows[0].cells.length; for (var i = 0; i < table.rows.length; i++) { var newCell = table.rows[i].insertCell(); newCell.contentEditable = true; newCell.innerHTML = "新单元格"; } }
时间: 2024-04-14 12:25:51 浏览: 243
根据你提供的代码,我看到你已经使用了`colgroup`和`col`元素来设置表格列的宽度。但是,你在添加行和列的函数中没有包括对`colgroup`和`col`元素的更新。
在`addRow()`和`addColumn()`函数中,你需要更新`colgroup`和`col`元素的数量,以匹配新添加的行和列。这样,表格的列宽度设置才会生效。
下面是修改后的代码,包括对`colgroup`和`col`元素的更新:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>鉴本</title>
<style>
table {
border-collapse: collapse;
}
td, th {
border: 1.5px solid black;
padding: 5px;
}
table img {
max-width: 150px;
height: auto;
}
.popup {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
display: flex;
align-items: center;
justify-content: center;
}
.popup img {
max-width: 100%;
max-height: 100%;
cursor: zoom-out;
}
</style>
</head>
<body>
<div>
<button onclick="addRow()">新增一行</button>
<button onclick="addColumn()">添加一列</button>
<button onclick="deleteRow()">删除行</button>
<button onclick="deleteColumn()">删除列</button>
<button onclick="changeSize()">调整大小</button>
</div>
<div id="popup" class="popup" style="display: none;">
<img id="popupImg">
</div>
<table id="myTable">
<thead>
<colgroup id="colGroup">
<col style="width: 40px;">
</colgroup>
<tr>
<th>表头1</th>
<th>表头2</th>
<th>表头3</th>
<!-- 其他表头列 -->
</tr>
</thead>
<tbody>
<tr>
<td>单元格1</td>
<td>单元格2</td>
<td>单元格3</td>
<!-- 其他单元格列 -->
</tr>
</tbody>
</table>
<script>
var table = document.getElementById("myTable");
var colGroup = document.getElementById("colGroup");
var imageColumnIndices = [8, 9, 10, 11, 12, 13];
function addRow() {
var newRow = table.insertRow();
var cellsCount = table.rows[0].cells.length;
for (var i = 0; i < cellsCount; i++) {
var newCell = newRow.insertCell();
newCell.contentEditable = true;
if (imageColumnIndices.includes(i)) {
var newInput = document.createElement("input");
newInput.type = "file";
newInput.onchange = function() {
previewImage(this);
};
newInput.onclick = function() {
showPopup(this.src);
};
newCell.appendChild(newInput);
} else {
newCell.innerHTML = "新单元格";
}
}
updateColGroup();
}
function addColumn() {
var cellsCount = table.rows[0].cells.length;
var col = document.createElement("col");
col.style.width = "40px";
colGroup.appendChild(col);
for (var i = 0; i < table.rows.length; i++) {
var newCell = table.rows[i].insertCell();
newCell.contentEditable = true;
newCell.innerHTML = "新单元格";
}
}
function updateColGroup() {
var colsCount = colGroup.getElementsByTagName("col").length;
var cellsCount = table.rows[0].cells.length;
if (colsCount < cellsCount) {
for (var i = colsCount; i < cellsCount; i++) {
var col = document.createElement("col");
col.style.width = "40px";
colGroup.appendChild(col);
}
} else if (colsCount > cellsCount) {
while (colsCount > cellsCount) {
colGroup.removeChild(colGroup.lastChild);
colsCount--;
}
}
}
</script>
</body>
</html>
```
在修改后的代码中,我添加了一个`updateColGroup()`函数,用于更新`colgroup`中`col`元素的数量。在`addRow()`和`addColumn()`函数中,我调用了`updateColGroup()`函数来确保`colgroup`与表格的列数保持一致。
希望这次的解答能够帮助你解决问题。如果还有其他疑问,请随时提问。
阅读全文