简体   繁体   English

JavaScript表缩小以适合其内容

[英]JavaScript table to shrink to fit its content

The following code produces cells with higher height that needed I want cells to fit its inner canvas in order to draw a diagonal on them 以下代码生成的单元格具有更高的高度,我需要这些单元格适合其内部画布才能在其上绘制对角线

 var miglobal = []; var ref1 = document.createElement("table"); var refx = document.createElement("tbody"); ref1.appendChild(refx); var ref2, ref3; console.log(ref1); for (var y = 0; y < 25; y++) { ref3 = refx.insertRow(y); for (var x = 0; x < 25; x++) { ref2 = ref3.insertCell(x); var cnv = document.createElement("canvas"); cnv.setAttribute("id", ("mycnv" + (y * 25 + x)).toString()); cnv.setAttribute("height", "30px"); cnv.setAttribute("width", "30px"); var cntx = cnv.getContext("2d"); cntx.moveTo(0, 0); cntx.lineTo(30, 30); cntx.stroke(); miglobal.push(cntx); ref2.appendChild(cnv); }; }; document.body.appendChild(ref1); 
 table { border: 2px black solid; } tr:hover { background: red; } tr >td { border: 2px solid black; padding: 0; } td > canvas { height: 100%; width: 100%; padding: 0; } 

All is generated dynamically because 25*25 at hand table could be cumbersome 所有这些都是动态生成的,因为手头上的25 * 25可能很麻烦

By default, the canvas element is displayed as an inline element. 默认情况下,canvas元素显示为嵌入式元素。 This causes the generation of a line box with the canvas element by default aligned to the baseline of the line box. 这将导致生成带有默认情况下的canvas元素与行框的基线对齐的行框。 The extra space you are encountering is the space below the baseline (ie space for characters such as "g" that descend below the baseline). 您遇到的多余空间是基线以下的空间(即,字符(例如“ g”)下降到基线以下的空间)。

In your example, you could change the canvas element to be displayed as a block element. 在您的示例中,您可以更改canvas元素以显示为块元素。 For example... 例如...

td > canvas {
  padding: 0;
  display: block;
}

Or you could change the canvas element to be displayed with a different vertical alignment. 或者,您可以更改canvas元素以不同的垂直对齐方式显示。 For example... 例如...

td > canvas {
  padding: 0;
  vertical-align: middle;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM