通过 bgcolor 属性选择元素
Posted
技术标签:
【中文标题】通过 bgcolor 属性选择元素【英文标题】:Selecting elements by bgcolor Attribute 【发布时间】:2019-07-20 17:29:04 【问题描述】:我想在其 CSS 包含特定背景颜色(bgcolor 属性)的表中选择一堆 td。我如何做到这一点?
我尝试过$("[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>
【问题讨论】:
【参考方案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>
【讨论】:
以上是关于通过 bgcolor 属性选择元素的主要内容,如果未能解决你的问题,请参考以下文章
匹配 TR 元素的 bgcolor 属性的 XPath 表达式
使用带有 bgcolor 属性的 getElementById()