JQuery removes everything except bold
I have html like this:
<div id="divTestArea1">
<b>Bold text</b>
<i>Italic text</i>
<div id="divTestArea2">
<b>Bold text 2</b>
<i>Italic text 2</i>
<div>
<b>Bold text 3</b>
</div>
</div>
and I would like to remove all elements that are not in bold. I tried with this code:
$('*:not(b)').remove();
and a couple more options, but they all either fail, or delete everything. btw, are jquery selectors and jsoup selectors 100% compatible? I would also like to use the answer in jsoup.
+5
5 answers
Your current code deletes the document <body>, as well as everything <div>containing tags <b>. If you want to save only bold text, the Shih-En Chou solution works well. If you want to keep the structure the <div>tags are in <b>, you can do this:
$("body *:not(div, b)")ââââ.remove();â
+5
:
<b> .
- >
- > <b> <body>
: http://jsfiddle.net/sechou/43ENq/
$(function(){
var tmpB = $("b").clone();
$('body').remove();
$("body").append(tmpB);
});â
+3