简体   繁体   English

动态添加表格行时如何更改td.style

[英]How to change td.style when dynamic add table row

I use JavaScript to add table rows dynamically, but I don't know how to change the style of the table data. 我使用JavaScript动态添加表行,但是我不知道如何更改表数据的样式。 My code is: 我的代码是:

var tableis = document.getElementById("cameraTable");
var last=(-1);
var Tr = tableis.insertRow(last);
var Td1 = Tr.insertCell(0);
Td1.innerHTML="<div name='mac"+i+"' id='mac"+i+"'>"+mac+"</div>";
Tr.appendChild(Td1);

and I only know how to change the background of the td: 而且我只知道如何更改td的背景:

Td1.bgColor="#00FF00";

I want to change the border style of the td, is it possible? 我想更改td的边框样式,可以吗? The style is like: 风格就像:

style="border: 1px solid black;"

I have tried Td1.style="border: 1px solid black;" 我已经尝试过Td1.style="border: 1px solid black;" but it is no effort. 但这不费力。

Any answer appreciated. 任何答案表示赞赏。

You can do this using the HTMLElement.style : 您可以使用HTMLElement.style来做到这一点:

Td1.style.border = '1px solid black';

Simple Demo Here 这里简单的演示

I think a more elegant solution would be to have the CSS class defined rather than hard-coding the style in the cell. 我认为更优雅的解决方案是定义CSS类,而不是在单元格中对样式进行硬编码。

/* CSS Class */
.cellStyle{
     border: 1px solid black;
     background-color: #00FF00;
}

/* JavaScript Code */
Td1.className = 'cellStyle';

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

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