簡體   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