如何解包多个父元素

how to unwrap the multiple parents elements

我正在从服务器解码 html。一旦我收到它看起来像这样:

它的跨度简单地嵌套在 no.of 跨度的

<p><span><span><span><span>The portacabin consist of toilets, <strong></strong> <strong>\u003e\u0026#160;</strong> showers and changing cubicles, a classroom, an office and a store room. <br>The
 upgrade shall include development of foot paths and installation of new
 up-lights around the area complete with all the required & 
necessary directional signages, as per the Standards Engineering 
Specification & approval by client.</span></span></span></span></p>

我想删除不需要的 span,即使它只有 space。怎么做?

我试过这个:

var p = $('p').contents().unwrap();

console.log(p);

有一个空的强元素,只有 space 有强元素 - 如何清除它?

解包步骤,就是这样。 有人帮帮我吗?

Live

您可以使用 .nodeType 属性 来测试文本级节点,然后将它们与空白正则表达式进行匹配 /\s*/:

if (this.nodeType === Node.TEXT_NODE) {
    this.innerText.match(/\s*/) && $(this).unwrap();
}

您可以使用 .each() 函数将其置于循环中,以展平 span-s 的每个级别,并在所有 spans 上迭代。这是 fiddle 显示的基本版本:http://jsfiddle.net/LwmoLp3o/2/