简体   繁体   English

从div中完全删除元素

[英]Remove element completely from div

I've got multiple svg with a common class. 我有一个通用类的多个svg。

<div id="holder">
   <svg width="100" height="100" class="circle">
     <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
   </svg>
</div>

How can I remove it completely? 如何将其完全删除? I tried .empty(); 我尝试了.empty(); and remove(); remove(); but did not work? 但是没用吗?

$('#removeBtn').click(function(){
    $('.circle').remove();
});

I am guessing you missed a DOM ready handler :) 我猜你错过了DOM ready处理程序:)

eg 例如

$(function(){
    $('#removeBtn').click(function(){
        $('.circle').remove();
    });
});

$(function(){}) is just a handy shortcut for $(document).ready(function(){}) $(function(){})只是$(document).ready(function(){})的便捷快捷方式

your html 您的html

   <div id="holder">
       <svg width="100" height="100" class="circle">
         <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
       </svg>
    </div>

<button id="removeBtn" >remove</button>

and js 和js

$('#removeBtn').click(function(){

    $('.circle').remove()
});

here the fiddle 这里的小提琴

https://jsfiddle.net/ptosn8w6/1/ https://jsfiddle.net/ptosn8w6/1/

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

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