亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 CSS 添加到使用 javascript 創建的動態表中

如何將 CSS 添加到使用 javascript 創建的動態表中

慕碼人8056858 2023-08-18 16:32:29
基本上我在 javascript 上創建一個動態表并將其顯示在 html 頁面中。我還有一個適用于靜態元素的 css 文件,但不適用于我動態創建的表格。我可以做什么來設計動態創建的表格的樣式?我已經嘗試過這個:table.classList.add('tablestyle');但它不起作用。正如你可以想象的那樣,我對這類事情絕對是新手。我有3個文件:reports.component.ts(包含 html。對 sccs 和 js 的引用位于文件中,并且它們可以正常工作)<html><div class="block">        <h1>STORICO DEI DATI</h1>            <div id="myDynamicTable" class="table">                <body onload="Javascript:addTable()"></body>            </div>        </div></html>報告.組件.jsfunction 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));               }report.component.scss(更長,但這是我想要工作的部分).tablestyle{  font-weight: bold;}
查看完整描述

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>


查看完整回答
反對 回復 2023-08-18
?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

添加

table.className = 'tablestyle'

或者

table.setAttribute("class", "tablestyle");


查看完整回答
反對 回復 2023-08-18
  • 2 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號