jquery - 通过 bgcolor 属性选择元素
问题描述
我想在其 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>
解决方案
当您想按属性值查找元素时,选择器需要双引号。
$('[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>
推荐阅读
- python - 将数据框中的每一行除以 Python 中的向量
- javascript - 如何只打开特定的模态窗口?
- python - 如何在 python 执行的 sql 语句中使用 hex(x ' ' )?
- javascript - 将代码移动到函数中时,React-leaflet LayersControl 引发错误
- vue.js - 选择单选按钮时,Vue组件不呈现条件内容
- c# - CodedUITest 前端交互使用另一个本地用户(模拟)
- javascript - 为文档生成自定义 dgeni 管道
- javascript - 统计动态行数并在html的输入框中显示
- c# - 需要精确到小数点后 3 位
- java - Keycloak admin rest api:未设置更新密码