通过bgcolor属性选择元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过bgcolor属性选择元素相关的知识,希望对你有一定的参考价值。

我想在一个表中选择一堆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>
答案

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

$('[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>

以上是关于通过bgcolor属性选择元素的主要内容,如果未能解决你的问题,请参考以下文章

匹配 TR 元素的 bgcolor 属性的 XPath 表达式

(Vue -03)Vue绑定样式 + 计算属性 + 侦听器+ 过滤器

jquery中的$的特殊用法

使用带有 bgcolor 属性的 getElementById()

Wix XmlFile 元素执行得太早

jQ选择器学习片段(JavaScript 部分对应)