简体   繁体   English

删除父Div,但不删除父内容。

[英]Remove the Parent Div, but not what's inside the parent.

Hey, I'm not sure if this is possible but anyway. 嘿,我不确定这是否可行,但无论如何。 Say for example: 比如说:

<div id="foo">
    <a href="#" id="bar">Remove Parent</a>
</div>

$(function() {
    $('#bar').click(function() {
        $(this).parent().remove();
    }); 
});

Is it possible that you can remove the parent container, in this example #foo but keep the child anchor tag #bar ? 您是否可以删除父容器,在此示例中为#foo但保留子锚标记#bar

In this situation you'd be looking for .unwrap() 在这种情况下,你一直在寻找.unwrap()

example... 例...

$(function() {
    $('#bar').click(function() {
        $(this).unwrap();
    });
});

This way: 这条路:

$(function() {
    $('#bar').click(function() {
        $("#bar").insertAfter("#foo");
    }); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM