简体   繁体   English

使用jQuery从父元素中删除div孩子

[英]removing div child from parent element with jquery

is it possible to remove all the div element only from the parent (with class name container) without removing the ul element inside the foo-1 class ? 是否可以仅从父级(带有类名容器)中删除所有div元素,而无需在foo-1类中删除ul元素?

<div class="container">
    <div class="foo-1">
        <ul class="toon">
            <li>li 1</li>
            <li>li 1</li>
        </ul>
    </div>
    <div class="foo-2">foo 2</div>
</div>

You can exclude any element containing such an UL 您可以排除包含此类UL的任何元素

 $('.container').children(':not(:has(ul.toon))').remove(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="foo-1"> <ul class="toon"> <li>li 1</li> <li>li 1</li> </ul> </div> <div class="foo-2">foo 2</div> </div> 

This keeps the entire .foo-1 element. 这将保留整个.foo-1元素。
If you wanted to remove the parent .foo-1 as well, you would have to call $('.toon').unwrap() 如果还要删除父.foo-1 ,则必须调用$('.toon').unwrap()

 $('.container').children(':not(:has(ul.toon))').remove(); $('ul.toon').unwrap(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="foo-1"> <ul class="toon"> <li>li 1</li> <li>li 1</li> </ul> </div> <div class="foo-2">foo 2</div> </div> 

Both of these would keep all events and associated data that might be attached to the element. 两者都将保留所有事件和可能附加到元素的关联数据。

I would clone the ul element, remove the div inside the container, and then append the ul back in. Not sure why you'd need to do that but if I needed to that is how I'd do it. 我将克隆ul元素,删除容器内的div,然后将ul附加回去。不知道为什么需要这样做,但是如果需要的话,我将如何做。

var toon = $(".toon").clone();

$(".container").children().remove();

$(".container").append(room);

It occurs to me two solutions: 我想到了两种解决方案:

  • First, put the content of class container into a variable, then erase the div with class container and add the contents of the variable to where that div was, 首先,将类容器的内容放入变量中,然后使用类容器擦除div并将变量的内容添加到该div所在的位置,
  • Second, if you only want to delete the css applied to the div with class container you can delete the class from that div. 其次,如果您只想删除带有类容器的应用于div的css,则可以从该div中删除该类。

I hope it helps you. 希望对您有帮助。

 $(".container").children().each(function(){ $(this).html(); $(this).parent().append($(this).html()); $(this).remove(); }); console.log($(".container").html()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="foo-1"> <ul class="toon"> <li>li 1</li> <li>li 1</li> </ul> </div> <div class="foo-2">foo 2</div> </div> 

First I'm traversing through all the children then appending their inner html to the parent ie. 首先,我遍历所有子项,然后将其内部html附加到父项(即)。 container.This will help you get all the html inside the child divs get out and append it to the parent.The following will remove all the containers only the html will be left remaining. 容器。这将帮助您获取子div中的所有html并将其附加到父对象。以下将删除所有仅保留html的容器。

 $(".container").children().each(function(){ $(this).unwrap(); }); console.log($(".container").html()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="foo-1"> <ul class="toon"> <li>li 1</li> <li>li 1</li> </ul> </div> <div class="foo-2">foo 2</div> </div> 

Try with unwrap(); 尝试使用unwrap(); Example : 范例:

$(".toon").unwrap();

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

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