Javascript 使用“document.createElement()”方法创建后,如何在javascript中设置单元格宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14395420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:38:12  来源:igfitidea点击:

how to set cell width in javascript just after creating it with 'document.createElement()' method?

javascripthtml

提问by user1976456

I've created cells for a dynamic table in html page using "document.createElement('td')" method & now wish to set width of each individual cell with some different value. Tried "cell.width = width" but it didn't work.

我已经使用“document.createElement('td')”方法在 html 页面中为动态表格创建了单元格,现在希望用一些不同的值设置每个单独单元格的宽度。试过“cell.width = width”但没有用。

how can I achieve it?

我怎样才能实现它?

回答by Minko Gechev

Use style.width:

使用style.width

var cell = document.createElement('td');
parent.appendChild(cell);
cell.style.width = '200px';

Here is an example.

这是一个例子

回答by 11684

To post my comment as an answer:

发表我的评论作为答案:

Use cell.style.width = width;.

使用cell.style.width = width;.