Bold text Italic text

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
source share
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();​

Demo

+5
source

:

<b> . - > - > <b> <body>

: http://jsfiddle.net/sechou/43ENq/

$(function(){
   var tmpB = $("b").clone();
   $('body').remove();
   $("body").append(tmpB);
});​
+3

#divTestArea2, div, #divTestArea1, , is not a <b>, :

$("#divTestArea1").append($("*", "#divTestArea2")).find('*').filter(function() {
    return this.tagName !== 'B';
}).remove();

FIDDLE

#divTestArea1 , , <b>, :

$('body')​.append($('b'))​.find('*')​.not('b')​.remove();​

FIDDLE

+2

.detach().

var $body = $("body");
var $b = $("b", $body).detach();
$(":not(b)", $body).remove();​​​​​​​​​​​
$body.append($b);

, -, , <b/>.

(demo)

+2

:

// Find all the <b> tags and unwrap them so they all become siblings and finally 
// remove non <b> siblings
$('body').find('b').unwrap().siblings('*:not(b)').remove();

: http://jsfiddle.net/3f2Hu/

+1

All Articles