You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于td内容为PowerShell生成的HTML表格行设置红色背景

解决方法

你可以直接在PowerShell生成的HTML代码中插入一段内嵌原生JavaScript脚本实现需求,不需要修改原有表格结构,也不需要依赖任何第三方库。

实现代码

<script>
document.addEventListener('DOMContentLoaded', function() {
  // 获取表格所有行
  const allRows = document.querySelectorAll('table tr');
  // 遍历行,跳过表头(第一行是th,无td元素)
  for (let i = 1; i < allRows.length; i++) {
    const currentRow = allRows[i];
    const firstTd = currentRow.querySelector('td:first-child');
    if (!firstTd) continue;
    // 判断第一个td内容是否为未勾选的方框
    if (firstTd.textContent.trim() === '☐') {
      // 可根据需求调整背景色、文字色
      currentRow.style.backgroundColor = '#ff4444';
      currentRow.style.color = '#ffffff';
    }
  }
});
</script>

相关说明

  • 脚本通过DOMContentLoaded事件触发,保证执行时表格已经完成渲染,不会出现找不到元素的问题。
  • 如果你需要适配多表格场景,可以调整选择器,例如仅匹配第一个表格的行可以改为document.querySelectorAll('table:first-of-type tr')。
  • 集成到PowerShell脚本时,可直接将上述代码作为ConvertTo-Html命令的-PostContent参数值传入,不需要手动修改生成后的HTML文件。

内容的提问来源于stack exchange,提问作者lapingultah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 13:15:02