javascript - 从多个嵌套位置删除没有内容的类的 div

标签 javascript jquery

我有:

<div class="stuff">
    <div class="removeme">
        <div class="iamfree">
            iamfree
            <div class="removeme">
                <div class="ishouldbefreetoo">
                    ishouldbefreetoo
                </div>
            </div>
        </div>
    </div>
</div>

我想要:

    <div class="stuff">
        <div class="iamfree">
            iamfree
                <div class="ishouldbefreetoo">
                    ishouldbefreetoo
                </div>
        </div>
    </div>

在这里尝试了 jQuery 的 unwrap() 函数:http://jsfiddle.net/adyz/7d947wja/

此外,克隆元素在展开时表现得很奇怪。

最佳答案

您需要调用unwrap()关于要删除的元素的内容。

$('.removeme').contents().unwrap()

演示:Fiddle

Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.

关于javascript - 从多个嵌套位置删除没有内容的类的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25792455/

相关文章:

javascript - 确定属性是否是 Chrome/Firefox 中的 native DOM 属性

javascript - 从数组中动态获取对象的值

javascript - 由于 node.js 延迟响应导致连接超时

javascript - 是否可以暂停 while 循环直到动画结束?

javascript - 更改函数的加载顺序会意外破坏代码

javascript - 在 Javascript 中使用数组

javascript - 清除所有 setIntervals

jquery - 淡入、淡出、淡入

javascript - 如何将文本从一页重复到另一页以减少每月更新的时间

javascript - 为什么没有调用 "focusin"事件处理程序?