该方法也无法修改第8列列宽
时间: 2023-12-28 13:03:15 浏览: 28
非常抱歉,我之前的回答中犯了一个错误。对于使用`contenteditable="true"`属性的单元格,无法直接通过CSS来设置其宽度。这是因为`contenteditable`属性会使单元格成为可编辑状态,并且会覆盖CSS中设置的宽度。
如果您想要修改第8列的列宽,可以使用JavaScript来实现。以下是一个示例,展示如何使用JavaScript动态设置第8列的宽度:
```html
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: separate;
width: 100%;
}
td, th {
border: 1.5px solid black;
padding: 5px;
}
td:nth-child(1) input,
td:nth-child(2) input,
td:nth-child(3) input,
td:nth-child(4) input,
td:nth-child(5) input,
td:nth-child(6) input,
td:nth-child(7) input {
width: 10%;
}
</style>
<script>
window.addEventListener('DOMContentLoaded', (event) => {
const td8 = document.querySelector('td:nth-child(8)');
td8.style.width = '300px';
});
</script>
</head>
<body>
<table>
<tr>
<td><input type="text" placeholder="Column 1"></td>
<td><input type="text" placeholder="Column 2"></td>
<td><input type="text" placeholder="Column 3"></td>
<td><input type="text" placeholder="Column 4"></td>
<td><input type="text" placeholder="Column 5"></td>
<td><input type="text" placeholder="Column 6"></td>
<td><input type="text" placeholder="Column 7"></td>
<td contenteditable="true">Column 8</td>
</tr>
</table>
</body>
</html>
```
在上述代码中,我添加了一个`DOMContentLoaded`事件监听器,当页面加载完成后,会获取第8列的`<td>`元素,并使用JavaScript将其宽度设置为300px。
请尝试使用修改后的代码,并检查第8列的宽度是否成功设置为300px。如果还有其他问题,请随时提供更多详细信息。非常抱歉给您带来的困扰。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)