繁体   English   中英

如何有条件地格式化我的 HTML 表格?

[英]How can I conditionally format my HTML table?

<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td>
</tr><tr class='detail-hide'><td Class='result-name '>pmu: COMMITTED_PKT_BSB</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1655.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1836.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1655.0</td>
<td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1836.0</td>

我有一个像上面这样的 HTML 表我正在尝试根据应用于数字的公式进行条件格式设置,我试过这个:

var tb = document.getElementByClass('metric')

我无法获得这些值 感谢您的任何修改或建议

您的代码的唯一问题是您使用错误的 js 上下文来搜索使用 js 的类。

document.getElementByClass('metric')

因为类可以超过 1,所以选择类的上下文有元素而不是像下面这样的元素它应该是元素(复数)而不是元素(单数)

document.get Elements ByClass('metric')

希望这能解决您的疑问。

如果需要任何其他帮助,请在此处发表评论,我会尽力解决

该方法是错误的 - 您要使用document.getElementsByClassName

var tb = document.getElementByClass("metric");

您还可以使用querySelectorAll仅获取具有类metric td元素:

var tb = document.querySelectorAll("td.metric");

有两个问题:

1)您错过了第一个<tr>和最后一个</tr>并且还必须将您的tr包裹在table tag 中。

2)改变:

document.getElementByClass('metric') ;    

到:

document.getElementsByClassName('metric') ;

 var tb = document.getElementsByClassName('metric') ; console.log(tb) ;
 <table> <tr> <td class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td> <td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td> <td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td> <td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1997.0</td> </tr> <tr class='detail-hide'><td Class='result-name '>pmu: COMMITTED_PKT_BSB</td> <td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1655.0</td> <td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1836.0</td> <td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1655.0</td> <td Class='metric' title='Test gave a performance metric.' lastPassTag=''>1836.0</td> </tr> </table>

暂无
暂无

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

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