如何使用Jquery删除表格单元格中的特定单词?

时间:2011-07-02 07:15:11

标签: jquery

如何使用Jquery从波纹管表格单元格中删除“def”。

<table>
    <tr>
        <td>abc def</td>
        <td>xyz def</td>
        <td>lmn def</td>
    </tr>
    <tr>
        <td>def</td>
        <td>abc def</td>
        <td>xyz def</td>
    </tr>
</table>

2 个答案:

答案 0 :(得分:5)

修改 你可以这样做:

$("td:contains(def)").each(function(){
  var newText = $(this).text().replace(/def/g, "");
  $(this).text(newText);
});

希望这会有所帮助。干杯

答案 1 :(得分:2)

您可以使用:contains()选择器并将函数传递给text()方法:

$("td:contains(def)").text(function(index, currentText) {
    return currentText.replace("def", "");
});