2013-06-24 82 views
2

我正在建立一個CMS,並有一個所見即所得的輸入文本;但是由於格式是從另一個文檔傳遞過來的,因此有時會得到一個隨機複製的<div>jQuery展開div內p

所以,更多的,往往不是HTML輸出一些標準文本看起來像:

<div class="show-text"> 
    <p>In Italo Calvino’s 1985 novel Mr. Palomar, the reader is taken on a journey through mundane but highly individual musings, seen from the perspective of the title character, who takes his name from the famous telescope and observatory. Mr Palomar tries to explain and define his existence by seeking order and reason in his visual and cultural surroundings. Taking the novel as a starting point, this exhibition has developed through close collaboration and discussion between the artists to create a symbiosis between the artworks, the boundaries of which are at times ambiguous.</p> 
    <p>Dylan Shipton’s work re-models the space through large-scale architectural interventions, including a viewing structure that provides an altered view of the other pieces. Shipton’s work will serve as an environmental framework and counter-point for the other works to react with and against. In an extension of her painting practice, Kate Brigden is showing a series of glazed porcelain objects that aim to toy with ideas of vision and perception, suggestive of cosmic concepts. Pippa Gatty’s video projections simulate a landscape and atmosphere, enveloping and interrupting the objects and structures.</p> 
<p>Palomar is developed from a project initiated at Surface Gallery, Nottingham 2012.</p> 
<p><div><em>‘In identifying a constellation, the decisive proof is to see how it answers&nbsp;</em><em>when you call it. More convincing than the matching of distances&nbsp;</em><em>and configurations with those marked on the chart is the reply that&nbsp;</em><em>the luminous dot gives to the name by which it has been called, its&nbsp;</em><em>promptness in responding to that sound, becoming one with it.’ &nbsp;—&nbsp;</em>Mr.&nbsp;Palomar, Italo Calvino, (Martin Secker &amp; Warburg, 1985)</div></p> 
    </div> 

所以,我很好奇地想知道,使用jQuery,我怎麼可以在解開任何不必要的<div>小號<p> s。我認爲這會工作:

$('.show-content-container .show-text p div').contents().unwrap(); 

但它只是在div前後刪除p。與.show-text.html()測試時

<p><div> 

How it looks to jQuery

<p></p><div> 

因此該選擇永遠做你

先進的感謝, [R

回答

1

如何您的標記看起來你希望因爲實際上沒有任何div在任何p的內部。

如果你不希望任何div的.show文本里面所有,你可以只使用:

$('.show-text').find('div').contents().unwrap(); 

jsFiddle here.

0

股利未在p呈現。

$('.show-text div').replaceWith(function() { 
    return '<p>' + $(this).html() + '</p>'; 
});