html - HTML动态表格 - 插入文本时停止扩展高度
问题描述
我有固定宽度和高度的动态表。使用随机函数我得到细胞数(3x3 - 7x7)。表渲染良好。在每个单元格上,我单击了将一个字母写入指向单元格的事件。有一个问题,因为行在高度上扩展。如何阻止它?
https://codepen.io/anon/pen/PdrNVM
let table = document.getElementById('table');
let tableSize = Math.floor((Math.random() * 5) + 3);
console.log(tableSize);
for (i = 0; i < tableSize; i++) {
let tr = document.createElement('tr');
for (j = 0; j < tableSize; j++) {
let td = document.createElement('td');
let content = document.createTextNode('');
td.appendChild(content);
tr.appendChild(td);
}
table.appendChild(tr);
}
let tds = document.querySelectorAll("td");
tds.forEach((td) => {
td.addEventListener("click", function() {
td.innerHTML = 'p';
});
});
table {
width: 200 px;
height: 200 px;
table-layout: fixed;
}
td {
background-color: green;
}
<h2>Click in cell</h2>
<table id='table'></table>
解决方案
只需设置一个适当的height
和max-height
喜欢的:
td {
background-color: green;
height: 50px;
max-height: 50px;
}
推荐阅读
- r - 使用 runner 包总结组
- sql-server - SSRS 数据库概念
- reactjs - 事件侦听器中的多个状态更改,如何不批处理 DOM 更新?
- amazon-web-services - AWS IAM服务查询
- javascript - 搜索完所有 1000 个数组后如何转到 API 的第二页
- swift - 如何优雅地找到 SKSpriteNode 的旋转
- python - 如何使用python浏览?
- c# - WPF DataGrid 单元格前景绑定不起作用
- c# - 如果值大于零,RDLC 报告更改字体颜色
- spring-boot - 有什么区别:CamelContext vs ExtendedCamelContext vs SpringCamelContext?