[英]How to add inline style in javascript?
document.getElementById("usersList").innerHTML.style="background-color:red" += "<th>" + newuser + "</th>";
使用这种语法,我无法获取table rows
的background-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.