简体   繁体   English

通过 JavaScript 设置 HTML 元素的 CSS 宽度

[英]Setting CSS width of HTML <td> element through JavaScript

I'm trying to make a progress-bar for each "td" element in a HTML table.我正在尝试为 HTML 表中的每个“td”元素制作一个进度条。 The table itself is generated through JavaScript, currently with random placeholder values.该表本身是通过 JavaScript 生成的,目前具有随机占位符值。 I would like each progress-bar to show it's relative percentage to the highest randomly generated value.我希望每个进度条都显示它与最高随机生成值的相对百分比。 Please see the image below:请看下图:

在此处输入图像描述

Currently what I have done is this:目前我所做的是:

// GAMES PLAYED
const games = Math.floor(Math.random() * 100);
const td2 = document.createElement("td");
td2.textContent = games;
td2.innerHTML = games + '<div class="progress-bar"><div class="filled-progress-bar"></div>'
tr.appendChild(td2);

I generate two "div"s, where the grey one you see in the 3rd image is the ".progress-bar", and the other one that is filled with green is the ".filled-progress-bar".我生成了两个“div”,您在第三张图像中看到的灰色的是“.progress-bar”,另一个用绿色填充的是“.filled-progress-bar”。 I then try to set the width of the ".filled-progress-bar" to this:然后我尝试将“.filled-progress-bar”的宽度设置为:

// Iterate each sorted rows and add the correct amount of fill
// Get the lowest and highest value of the sorted row
const progressBarFirstValue = sortedRows[0].cells[column].textContent;
const progressBarSecondValue = sortedRows[sortedRows.length-1].cells[column].textContent;

const progressBarMaxValue = (progressBarFirstValue > progressBarSecondValue) ? progressBarFirstValue : progressBarSecondValue;

sortedRows.forEach(function (row) {
    const currentTdElement = row.cells[column];
    tdValue = currentTdElement.textContent;
    
    percentageOfMaxValue = tdValue / progressBarMaxValue * 100;

    currentTdElement.style.width = "percentageOfMaxValue%";
});

I would like to highlight the last line, where I use the.style.width command:我想突出显示最后一行,我使用 .style.width 命令:

currentTdElement.style.width = "percentageOfMaxValue%"; currentTdElement.style.width = "percentageOfMaxValue%";

This works if I manually insert it into my.css file, but it appears not to work when generated through my JavaScript file above.如果我手动将其插入到 my.css 文件中,则此方法有效,但通过上面的 JavaScript 文件生成时,它似乎不起作用。 Is there any chance that some of you can see where I go wrong?你们中的一些人有没有机会看到我的 go 哪里错了? Thank you so much for your time!非常感谢您的参与!

I fixed it somehow myself with some trial and error.我通过一些试验和错误以某种方式自己修复了它。 I changed the innerHTML content of the "td" element in the table to this:我将表中“td”元素的 innerHTML 内容更改为:

    td2.innerHTML = games + '<div class="progress-bar"></div><div class="filled-progress-bar"></div>';

And then when trying to calculate the "fill" for the progressbar I did this:然后在尝试计算进度条的“填充”时,我这样做了:

   const progressBarFirstValue = sortedRows[0].cells[column].textContent;
const progressBarSecondValue = sortedRows[sortedRows.length - 1].cells[column].textContent;

const progressBarMaxValue = (progressBarFirstValue > progressBarSecondValue) ? progressBarFirstValue : progressBarSecondValue;

sortedRows.forEach(function (row) {
    const currentTdElement = row.cells[column];
    tdValue = currentTdElement.textContent;
    percentageOfMaxValue = tdValue / progressBarMaxValue * 100;

    stringValue = percentageOfMaxValue.toString();
    
    currentTdElement.childNodes[2].style.width = stringValue + "%";


});

I think my issue was partly that in my original code, I couldnt access the ".filled.progress.bar" because it was nested inside the ".progress.bar" class, and my syntax didnt properly extract this div element.我认为我的问题部分是在我的原始代码中,我无法访问“.filled.progress.bar”,因为它嵌套在“.progress.bar”class 中,并且我的语法没有正确提取这个 div 元素。 Secondly, the formating of the "percentageOfMaxValue" was not done right, but the solution above seems to have fixed it.其次,“percentageOfMaxValue”的格式不正确,但上面的解决方案似乎已经修复了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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