gpt4 book ai didi

jquery - 从字符串中删除所有出现的 anchor 标记,同时保留其内部文本节点

转载 作者:行者123 更新时间:2023-12-01 04:29:32 25 4
gpt4 key购买 nike

我有一个 HTML 字符串。我想改变这个:

<table >
<tr>
<td><a href="/link.html" onclick="javascript:aFunction()">some text</a></td>
<td><a href="/anotherlink.html">some more text</a></td>
</tr>
</table>

进入此:

<table >
<tr>
<td>some text</td>
<td>some more text</td>
</tr>
</table>

最佳答案

在 jQuery 1.4+ 中,您可以将函数传递给 .replaceWith() ,像这样:

​$("table a")​.replaceWith(function() { return this.innerHTML; });​

You can give it a try here .

如果你确实有一个字符串,而不是元素,它看起来像这样:

var html = '<table>...{rest of string}...</table>';
var o=$(html).find('a').replaceWith(function(){ return this.innerHTML; }).end();​

You can try that version here .

关于jquery - 从字符串中删除所有出现的 anchor 标记,同时保留其内部文本节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3730886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com