我有一个关于从图像标签之外的字符串中删除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;">
提前致谢。
最佳答案
$('span')。contents()。unwrap();
$('p')。contents()。unwrap();
对于除图像以外的所有元素:$('*').not('img').contents().unwrap();
更多内容:
http://api.jquery.com/contents/
http://api.jquery.com/unwrap/