[英]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.