【问题标题】:How to replace content of a td having certain text in a table using Jquery?如何使用 Jquery 替换表中具有特定文本的 td 的内容?
【发布时间】:2016-02-04 15:53:55
【问题描述】:

我想替换 td 在表格中具有特定文本的内容。

这是我正在使用的表结构。

<table id="quotes" width="100%" cellspacing="0" cellpadding="2">
<tbody>
<tr class="rowEven">
    <td>Store Pickup Store Pickup</td>     <!-- td content to be replaced by "hello" -->
    <td class="cartTotalDisplay">$0.00</td>
</tr>
</tbody>
</table>

我想把上面提到的内容换成“你好”

【问题讨论】:

  • $('.rowEven td:first').html('Hello');
  • @Tushar 我必须通过它的内容来获取 td,而不是:first
  • 使用td:contains('your_Text')

标签: jquery html html-table


【解决方案1】:

您可以为此使用 :contains() 选择器

$("td:contains('Store Pickup Store Pickup')").text('hello');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="quotes" width="100%" cellspacing="0" cellpadding="2">
  <tbody>
    <tr class="rowEven">
      <td>Store Pickup Store Pickup</td>
      <td class="cartTotalDisplay">$0.00</td>
    </tr>
  </tbody>
</table>

【讨论】:

    【解决方案2】:
    $('#quotes tbody tr').find("td:contains('Store Pickup Store Pickup')").text('hello');
    

    使用:contains()

    描述:选择所有包含指定文本的元素。

    demo

    【讨论】:

      【解决方案3】:

      试试看:

        $('#quotes tbody tr td:first-child').html('Hello');
      

      【讨论】:

      • i have to get td by its content not as :first 由 OP 评论
      猜你喜欢
      • 2018-01-25
      • 2014-03-06
      • 1970-01-01
      • 2011-08-08
      • 2017-03-15
      • 1970-01-01
      • 2012-02-07
      • 2012-06-26
      • 1970-01-01
      相关资源
      最近更新 更多