从字符串中除去图像标记以外的html标记

Remove html tags apart from the image tag from the string

本文关键字:html 标记 字符串 图像      更新时间:2023-09-26

我有一个关于从字符串中除去图像标签的html标签的问题。假设我们有一个这样的字符串:

<p>MA2: Allow time for editing and other types of question revisions.<span class="AM"> <img src="img" title="{(1 if x>=0),(0 if x<0):}"style="vertical-align: middle;"> </span> <span class="AM"> </span> </p>

在这里,我们只需要删除<p><span>像html标签。但是我们需要图像标签


预期结果:

MA2: Allow time for editing and other types of question revisions.<img src="img" title="{(1 if x>=0),(0 if x<0):}" style="vertical-align: middle;">

$(跨度).contents () .unwrap ();

$("p").contents () .unwrap ();

对于除图像以外的所有元素:

$('*').not('img').contents().unwrap();

更多:http://api.jquery.com/contents/

http://api.jquery.com/unwrap/