[英]Jquery remove everything except for bolded
我有這樣的HTML:
<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>
我想刪除所有非粗體的元素。 我試過這段代碼:
$('*:not(b)').remove();
以及其他一些變化,但它們都會出錯或刪除所有內容。 順便說一下,jquery選擇器和jsoup選擇器是否100%兼容? 我也想在jsoup中使用這個答案。
您當前的代碼刪除了文檔<body>
以及包含<b>
標記的所有<div>
。 如果您只想保存粗體文本,那么Shih-En Chou的解決方案效果很好。 如果你想保存<b>
標簽所在的<div>
結構,你可以這樣做:
$("body *:not(div, b)").remove();
我的解決方案
我克隆<b>
並將其保存到內存中。 - >全部刪除 - >將<b>
插入<body>
這是我的代碼: http : //jsfiddle.net/sechou/43ENq/
$(function(){
var tmpB = $("b").clone();
$('body').remove();
$("body").append(tmpB);
});
試試這個:
// 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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.