简体   繁体   中英

How to add inline style in javascript?

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

With this syntax I am not able to get the background-color of my table rows .

Could you please give me the valid syntax to my question?

These are two different statements, also style is not a prpoerty on .innerHTML but on the element itself.

Also it is not a great idea to overwrite the complete style attribute, instead set the specific properties.

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

这是一种添加样式的方法

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

You are missing these two points:

  • backgroundColor is applied to the style property of the element
  • innerHTML is used to add HTML content in the element.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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