jQuery字符串替换和链接

时间:2016-08-07 19:05:16

标签: jquery replace hyperlink

我使用text.replace

替换表格单元格中的文本
$('td:contains("ABC ")').text(function(_, text) {
  return text.replace(/ABC /g, 'XYZ');
  return text.replace('ABC ', 'XYZ');
});

表示此类单元格ABC <a href="http://example.com">Text</a></td>

由于某些原因,当我应用该替换时,单元格内的链接对鼠标单击变为非活动状态。原因是什么,有可能改变它吗?

1 个答案:

答案 0 :(得分:2)

仅替换文本节点:

$('td:contains("ABC ")').contents().filter(function() {
    return this.nodeType == 3;
}).each(function() {
    this.textContent = this.textContent.replace(/ABC /g, 'XYZ');
});

Reference