通过bgcolor属性选择元素

问题描述 投票:0回答:1

我想在一个表中选择一堆td,其CSS包含特定的背景颜色(bgcolor属性)。我该如何实现这一目标?

我试过$("[bgcolor=#FF0000]"),但我得到了错误。 Uncaught DOMException: Failed to execute '$' on 'CommandLineAPI': '[bgcolor=#FF0000]' is not a valid selector.

<table>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td bgcolor="#FF0000">January</td>
    <td bgcolor="#00FF00">$100</td>
  </tr>
</table>
jquery jquery-selectors
1个回答
1
投票

当您想要按属性值查找元素时,选择器需要双引号。

$('[bgcolor="#FF0000"]').each(function(e){
	$(this).text('wow my bg is red');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td bgcolor="#FF0000">January</td>
    <td bgcolor="#00FF00">$100</td>
  </tr>
</table>
© www.soinside.com 2019 - 2024. All rights reserved.