我正在从服务器解码html
。一旦我收到,看起来像这样:
它的跨度简单地嵌套在多个跨度内
<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
,即使它只有空间
。如何做到这一点?
我尝试过这个:
var p = $('p').contents().unwrap();
console.log(p);
有一个空的强元素,并且只有空格
和强元素 - 如何清除它?
它解开步骤,就是这样。 有人可以帮帮我吗?
最佳答案
您可以使用 .nodeType
属性来测试文本级节点,然后将它们与空白正则表达式 /\s*/
进行匹配:
if (this.nodeType === Node.TEXT_NODE) {
this.innerText.match(/\s*/) && $(this).unwrap();
}
您可以将其放入带有 .each()
函数的循环中,以展平 span
-s 的每个级别,并在所有 span< 上进行迭代
s。这是一个显示此基本版本的 fiddle :http://jsfiddle.net/LwmoLp3o/2/
关于javascript - 如何解开多个父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27776948/