【问题标题】:Selecting elements by bgcolor Attribute通过 bgcolor 属性选择元素
【发布时间】: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>

【问题讨论】:

    标签: jquery jquery-selectors


    【解决方案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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 2013-12-21
      • 2013-10-25
      • 1970-01-01
      • 2010-11-02
      • 2021-03-14
      相关资源
      最近更新 更多