2 回答

TA貢獻1982條經驗 獲得超2個贊
為您制作了一段代碼,以向您展示它的工作原理。也許您的實際樣式表存在矛盾。
請注意將body
標簽放置在正確的層次結構中。
function addTable() {
? ? let colonne = Math.floor(Math.random() * 5)+2;??
? ? let righe = Math.floor(Math.random() * 5)+2;??
? ??
? ? var today = new Date();
? ? var dd = String(today.getDate()).padStart(2, '0');
? ? var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
? ? var yyyy = today.getFullYear();
? ??
? ? today = mm + '/' + dd + '/' + yyyy;
? ??
? ? let providers = ["p1", "p2", "p3", "np4", "p5"];
? ? let testcases = ["tc1", "tc2", "tc3", "tc4", "tc5"];
? ??
? ? ??
? ? var myTableDiv = document.getElementById("myDynamicTable");
? ? ??
? ? var table = document.createElement('table');
? ? table.classList.add('tablestyle');
? ??
? ? var tableBody = document.createElement('TBODY');
? ? table.appendChild(tableBody);
? ?
? ??
? ? for (var i=0; i<righe; i++){
? ? ? ?var tr =? document.createElement('tr');
? ? ? ?tr.style.backgroundColor = 'red';
? ? ? ?tableBody.appendChild(tr);
? ? ? ?
? ? ? ?for (var j=0; j<colonne; j++){
? ? ? ? ? ?var td = document.createElement('td');
? ? ? ? ? ?td.width='75';
? ? ? ? ? ?if(i==0){?
? ? ? ? ? ? ? ? if(j==0){ //prima casella
? ? ? ? ? ? ? ? ? ? addCell(td, tr, today);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else { //prima riga
? ? ? ? ? ? ? ? ? ? addCell(td, tr, providers[j-1]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ?}
? ? ? ? ? ?else {
? ? ? ? ? ? ? ?if(j==0){ //prima colonna
? ? ? ? ? ? ? ? ? ? addCell(td, tr, testcases[i-1]);
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?else {
? ? ? ? ? ? ? ? ? ? addCell(td, tr, Math.floor(Math.random() * 50));
? ? ? ? ? ? ? ?}
? ? ? ? ? ??
? ? ? ? ? ?}
? ? ? ??
? ? ? ? ??
? ? ? ?}
? ? }
? ? myTableDiv.appendChild(table);
? ??
}
function addCell(td, tr, valoreCella){
? ? td.appendChild(document.createTextNode(valoreCella));
? ? tr.appendChild(td);
}
.tablestyle{
? font-weight: bold;
? color: green
}
<body onload="addTable()">
? <div class="block">
? ? ? <h1>STORICO DEI DATI</h1>
? ? ? <div id="myDynamicTable" class="table">
? ? ? ? ? <!--JS-->
? ? ? </div>
? </div>
</body>

TA貢獻1786條經驗 獲得超11個贊
添加
table.className = 'tablestyle'
或者
table.setAttribute("class", "tablestyle");
添加回答
舉報