jQuery 在 p 中展开 div

标签 jquery

我正在构建一个 CMS,并且有一个所见即所得的输入文本;但由于格式是从另一个文档传递过来的,有时您会得到一个随机的 <div>复制过来。

因此,某些标准文本的 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> s 在 <p> 内s。我认为这会起作用:

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

但它只是删除了 div 前后的 p。

最佳答案

您的标记看起来如何:

<p><div>

How it looks to jQuery使用 .show-text.html() 进行测试时:

<p></p><div>

因此,该选择器永远不会按照您的意愿行事,因为任何 p 内实际上都没有任何 div。

如果你根本不想在 .show-text 中包含任何 div,你可以使用:

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

jsFiddle here.

关于jQuery 在 p 中展开 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17271884/

相关文章:

javascript - 获取之前的 URL 并在 Javascript 中显示在页面上

jquery - 选择带有文本内容的元素?

javascript - 防止多重事件触发

jquery - 如果 jQuery 找到 0 个匹配项,它会继续过滤吗?

javascript - Backbone.js - 无法触发集合中的事件

javascript - 无法访问 Javascript 中的 html 元素

javascript - 禁用 textarea 中由 javascript 定义的 keydown 事件并恢复默认行为

javascript - Jquery部分隐藏div

javascript - 切换滑动按钮在数据表中丢失位置

php - 如何获取js文件中的路由路径