使用 bgcolor 隐藏所有表格行

问题描述

我有一个长表,其中一些有 bgcolor 属性

<tr bgcolor="#EAE5C2">

我想用js隐藏这些行。 有没有办法只定位这一行并可能应用 display: none; css?

解决方法

在 CSS 中你可以使用属性选择器

tr[bgcolor] { display: none }

否则在 Javascript 中通过 document.querySelectorAll('tr[bgcolor]') 选择所有元素

例如

document.querySelectorAll('tr[bgcolor]').forEach(
  (r) => r.style.display = 'none';
);