2012-02-09 44 views
0

我怎樣才能去除這個div以外的p標籤(我做不是想要刪除測試div)。jQuery刪除標籤的任一側

<p> 
    <div class='test'> 
    content here 
    <img /> 
    </div> 
</p> 

我想會是這個結果......

<div class='test'> 
    content here 
    <img /> 
</div> 

我知道有一個類似的問題在這裏:jQuery: How do I remove surrounding div tags?,但沒有得到它在我的處境工作

我ve已嘗試

$('p .test').replaceWith($('.test)); 

但當然,那只是選擇沙龍幻燈片div,而不是p bef礦石它。

+0

可能重複的[刪除父分區,但不是內部的東西](http://stackoverflow.com/questions/5201834/remove-the-parent-div-but-not-whats-inside-the -parent) – 2012-02-09 11:38:36

+0

順便說一句。在'p'裏面有'div'是無效的。 – 2012-02-09 11:38:56

回答

0

這樣做,但請記住它會影響所有div class="test"

$("div.test").unwrap(); 
+0

非常感謝,如果它幫助任何人,我也必須確保它被$(document).ready解僱。 – SparrwHawk 2012-02-09 11:56:23

+0

是的,你可以使用'$(document).ready(function(){...})結構或更簡單的'$(function(){...})'。 – 2012-02-09 12:04:46

0

嘗試以下操作:

$('.salon-slideshow').each(function() { 
    $(this).parent().replaceWith($(this)); 
}); 
-1

試試這個:

$('.test').each(function() { 
    $(this).insertAfter($(this).parent()); 
    $(this).prev().remove(); 
}); 
+0

-1:因爲.unwrap()是一種更好,最常見的方法。 – 2012-02-09 11:45:41

+0

以前從未使用unwrap()。謝謝(你的)信息。還是我的回答是不正確的,只是它不是最好的答案.. :) – techfoobar 2012-02-10 03:15:24