简体   繁体   中英

Remove html tags apart from the image tag from the string

I have a question regarding remove the html tags from string apart from the image tag. Let's say we have a string like :

<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>

In this we only need to remove the <p> and <span> like the html tags. But we need the image tag.


Expected Result :

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;">

Thanks in advance.

$('span').contents().unwrap();

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

For all elements except images:

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

More here:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM