最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery remove everything except for bolded - Stack Overflow

programmeradmin2浏览0评论

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 aren't bold. I've tried with this code:

$('*:not(b)').remove();

and a couple other variations but they all either error out or remove everything. btw, are jquery selectors and jsoup selectors 100% patible? I'd like to use the answer to this in jsoup as well.

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 aren't bold. I've tried with this code:

$('*:not(b)').remove();

and a couple other variations but they all either error out or remove everything. btw, are jquery selectors and jsoup selectors 100% patible? I'd like to use the answer to this in jsoup as well.

Share Improve this question asked Aug 17, 2012 at 5:45 No_nameNo_name 2,8203 gold badges34 silver badges49 bronze badges 2
  • Here is something similar to your question -- stackoverflow./questions/3083225/… – Shekhar Commented Aug 17, 2012 at 5:55
  • In your example the <b> elements don't contain other elements, but what do you want to do if a particular <b> has, say, a nested <i>? – nnnnnn Commented Aug 17, 2012 at 6:34
Add a ment  | 

5 Answers 5

Reset to default 5

Your current code removes the document <body> as well as all <div>s which contain the <b> tags. If you only want to save the bold text then Shih-En Chou's solution works well. If you want to save the <div> structure that the <b> tags are in as well you could do this:

$("body *:not(div, b)")​​​​.remove();​

DEMO

My solution:

I clone <b> and save it into memory. ->Remove all -> insert <b> into <body>

here is my code: http://jsfiddle/sechou/43ENq/

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

Move all elements in #divTestArea2 as it is a div and will be removed as well to #divTestArea1, then filter out anything that is'nt a <b> and remove it :

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

FIDDLE

The above keeps the #divTestArea1 element intact, to remove everything but the <b> elements, something like :

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

FIDDLE

I prefer .detach().

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

This way you don't need to either move or clone anything to overe the problem of the deletion of the objects wrapping your <b/> elements.

(demo)

Try this:

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

Demo: http://jsfiddle/3f2Hu/

发布评论

评论列表(0)

  1. 暂无评论