【问题标题】:How do I check if td:nth-child(3) is greater than or equal to a percentage?如何检查 td:nth-child(3) 是否大于或等于百分比?
【发布时间】:2020-01-09 12:46:54
【问题描述】:

我目前正在编写一个包含所有这些数据的网页。在此数据中有 3 个单独的表。在每个表中,我想检查第三个td 中的TR 是否大于或= 百分比。

如何检查{{opendiff}}(已经是一个百分比)是否大于或等于 18%(这个百分比可以硬编码。)。该行应类似于:

goal | actual | % diff

100%  | 43% | 57%

如果 diff% 低于 50%,我希望它使用 jquery 到 toggleClass(),这样我可以使行变成红色(我已经为这些 TR 行创建了类,但我不知道如何实施检查以查看是否大于等)。

<tr>
    <td class="hide-on-large-only">Dialer</td>
    <td>18 %     </td>
    <td>{{opentransfer}}</td>
    <td>{{opendiff}}</td>
    <td class="hide-on-med-and-down"></td>
</tr>

我找到了我的 jquery 函数 jQuery toggleClass() 方法。我只是不知道如何检查 TD 是否大于 % 以生成 jquery toggleClass()。

【问题讨论】:

    标签: jquery node.js express handlebars.js


    【解决方案1】:

    甚至不需要 jQuery:

    const cells = Array.from(document.querySelectorAll(".opendiff"));
    
    // fill in random values
    for(const cell of cells) {
        cell.innerHTML = (Math.round(Math.random()*1000)/10) + " %";
    }
    
    // I set t to 50 to make it more likely for red values
    const maxPercentage = 50;
    // for each cell, as above
    for(const cell of cells) {
        cell.classList.toggle("red",parseFloat(cell.textContent)<maxPercentage);
    }
    table td {
     background-color: #cceeFF;
    }
    td.red {
      background-color: #FFDDDD;
    }
    <table>
    <tbody>
    <tr>
        <td class="hide-on-large-only">Dialer</td>
        <td>18 %     </td>
        <td>{{opentransfer}}</td>
        <td class="opendiff">{{opendiff}}</td>
    </tr>
    <tr>
        <td class="hide-on-large-only">Dialer</td>
        <td>18 %     </td>
        <td>{{opentransfer}}</td>
        <td class="opendiff">{{opendiff}}</td>
    </tr>
    <tr>
        <td class="hide-on-large-only">Dialer</td>
        <td>18 %     </td>
        <td>{{opentransfer}}</td>
        <td class="opendiff">{{opendiff}}</td>
    </tr>
    <tr>
        <td class="hide-on-large-only">Dialer</td>
        <td>18 %     </td>
        <td>{{opentransfer}}</td>
        <td class="opendiff">{{opendiff}}</td>
    </tr>
    </tbody>
    </table>

    【讨论】:

    • 我现在要试试这个,你把功能放在哪里了?就像 const cells =..... 直接进入我的 app.js 对吧?
    • 是的,并确保它在文档准备好后运行,例如。使用$(document).ready。显然也没有用随机值填充它的部分。
    • 我把它放在我的 app.js 中。它不起作用。我把它放在我的 app.get 路线上方。我可以把它放在 HBS 文件中并放入 吗?我很困惑
    • 您需要确保该脚本包含在您的 HTML 中并在您的页面加载后运行。从您的评论看来,您不确定如何在网页中包含脚本。如果是这样的话,你真的需要先谷歌一些 HTML/javascript 的基础知识。
    • &lt;script&gt; $(document).ready(function () { // Cell Logic const cells = Array.from(document.querySelectorAll(".opendiff")); // I set 50 for red values const maxPercentage = 50; // for each cell, as above for (const cell of cells) { cell.classList.toggle("red", parseFloat(cell.textContent) &lt; maxPercentage); } }); &lt;/script&gt; 我错过了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多