这样修改后列宽还是未变
时间: 2023-10-10 12:06:38 浏览: 73
非常抱歉之前的回答还是没有解决你的问题。在你的代码中,我注意到你将`col`元素放置在了`thead`标签中,而实际上应该将其放置在`table`标签中。
以下是修正后的代码,将`col`元素放置在`table`标签中,并为第二列和第三列设置宽度为100px:
```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">
<colgroup>
<col style="width: 40px;">
<col style="width: 100px;">
<col style="width: 100px;">
</colgroup>
<thead>
<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 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 = "新单元格";
}
}
}
function addColumn() {
var cellsCount = table.rows[0].cells.length;
var colgroup = table.getElementsByTagName("colgroup")[0];
var col = document.createElement("col");
col.style.width = "100px";
colgroup.appendChild(col);
for (var i = 0; i < table.rows.length; i++) {
var newCell = table.rows[i].insertCell();
newCell.contentEditable = true;
newCell.innerHTML = "新单元格";
}
}
</script>
</body>
</html>
```
希望这次能够解决你的问题。如果还有其他疑问,请随时提问。非常抱歉给你带来了困扰。
阅读全文