简体   繁体   English

用javascript动态生成表时如何在表单元格中插入空间

[英]How to insert space in table cell when generating table dynamically by javascript

i wrote a code to test one issue and notice that not being able to add space when generating table with data dynamically by javascript. 我编写了一个代码来测试一个问题,并注意到在通过JavaScript动态生成带有数据的表时无法添加空间。

i use to add some space but did not work. 我用来添加一些空间,但是没有用。

full code here 完整的代码在这里

<div id='table'>

</div>

var array = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}];
var arrayLength = array.length;
var theTable = document.createElement('table');

// Note, don't forget the var keyword!
for (var i = 0, tr, td; i < arrayLength; i++) {
    tr = document.createElement('tr');
    td = document.createElement('td');
    td.appendChild(document.createTextNode('<span>'+array[i].id+'&nbsp;&nbsp;</span>'));
    tr.appendChild(td);

    td = document.createElement('td');
    td.appendChild(document.createTextNode('<span>'+array[i].foo+'&nbsp;&nbsp;</span>'));
    tr.appendChild(td);

    theTable.appendChild(tr);
}
//alert(theTable);
document.getElementById('table').appendChild(theTable);

looking for suggestion. 寻找建议。 thanks 谢谢

You can continue the same way you did with td and tr. 您可以像使用td和tr一样继续进行操作。 Create a new span element, then set the innerHTML. 创建一个新的span元素,然后设置innerHTML。 createTextNode is just for that, text, so the browser won't interpret tags and html entities. createTextNode仅用于表示文本,因此浏览器不会解释标签和html实体。

var span = document.createElement('span');
span.innerHTML = array[i].id+'&nbsp;&nbsp;';
td.appendChild(span);

You can use the Unicode character for a space directly: 您可以直接将Unicode字符用于空格:

td.appendChild( document.createTextNode( '\u00A0' ) );

Can obviously be done a couple of times: 显然可以做几次:

td.appendChild( document.createTextNode( '\u00A0\u00A0\u00A0' ) );
// three spaces

You could use CSS to add some padding. 您可以使用CSS添加一些填充。

 #table td > span { padding-right:20px; } 

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

相关问题 如何在表单元格中动态插入div? - How to dynamically insert div in a table cell? javascript:如何将div动态添加到表格单元格? - javascript: How to add div to table cell dynamically? 当动态生成表行时,对齐表单元格的Javascript不起作用 - Javascript to align Table Cell is not working when table rows are dynamically generated 在Javascript中将图像插入表格单元格 - Insert image into table cell in Javascript 使用纯JavaScript动态隐藏表格单元格内容时,如何保护单元格边框? - How can I protect the cell borders when hiding the table cells contents dynamically using plain javascript? 如何在 javascript 中动态地从表格单元格中获取值 - how to get values from table cell dynamically in javascript 旋转表格单元格时如何消除空白? - How to eliminate the white space when I rotate a table cell? 如何使用JavaScript在Word文档中的表的特定单元格中插入图像 - How to insert image in a particular cell of a table in word doc using JavaScript 如何动态地将html表格单元格数据插入到React中带有动态标题的表格中? - How do I dynamically insert html table cell data into a table with dynamic headers in React? Javascript动态创建带有按钮单元格的HTML表 - Javascript Dynamically build HTML table with button cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM