javascript - 从 HTML 内容 Jquery 中去除特定的 span 标签

标签 javascript jquery html regex strip

我有以下 HTML 字符串,我只需要去除特定的跨度标签(具有 pre 属性的标签),但保留跨度内容

"<P>it <SPAN class='pre="it' ?>is is</SPAN> a movie</P>"

我需要以下输出,

"<P>it is is a movie</P>"

最佳答案

那么,首先您应该尝试使用有效的 HTML。然后你可以使用这段代码:

$("<p>it <span class='pre'>is is</span> a movie</p>")
    .find('span') //Used 'span' here since you'll probably change the class name
    .contents()
    .unwrap()
    .parent();

http://jsfiddle.net/T6cSe/


来自jQuery doc :

The .unwrap() method removes the element's parent. This is effectively the inverse of the .wrap() method. The matched elements (and their siblings, if any) replace their parents within the DOM structure.

您需要定位 span 的内容以展开 span 本身。

关于javascript - 从 HTML 内容 Jquery 中去除特定的 span 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22479651/

相关文章:

jquery - 将 Logo 放在 Bootstrap 导航栏中

java - 如何在 Struts 2 中使用 jQuery Ajax 检查唯一用户?

java - 是否可以附加 2 个富文本字符串?

javascript - WCF Restfull WS post方法和HTML表单

javascript - 纯 Javascript 监听输入值变化

javascript - Canvas 绘制图像

javascript - 在按钮按下/事件触发器上传递选定的对象模型

javascript - 删除包含星号或点的单词

javascript - jquery如何到达对象的末尾

javascript - 如何在 HTML 表单中抑制或转换弯引号等字符?