繁体   English   中英

如何在JavaScript中添加内联样式?

[英]How to add inline style in javascript?

document.getElementById("usersList").innerHTML.style="background-color:red" += "<th>" + newuser + "</th>";

使用这种语法,我无法获取table rowsbackground-color

您能给我我问题的有效语法吗?

这是两个不同的语句,样式也不是.innerHTML的属性,而是元素本身的属性。

覆盖完整的style属性,而是设置特定的属性,也不是一个好主意。

document.getElementById("usersList").style.backgroundColor="red";    
document.getElementById("usersList").innerHTML += "<th>" + newuser + "</th>";

这是一种添加样式的方法

document.getElementById("usersList").style.backgroundColor = "red";

您缺少这两点:

  • backgroundColor应用于元素的style属性
  • innerHTML用于在元素中添加HTML内容。

 var newuser = 'someUser'; document.getElementById("usersList").style.backgroundColor="red"; document.getElementById("usersList").innerHTML += "<th>" + newuser + "</th>"; 
 <table id='usersList' border='1'></table> 

暂无
暂无

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

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