How to clear all
s’ contents inside a parent
?

前端 未结 14 1872
面向向阳花
面向向阳花 2020-12-07 10:10

I have a div

which has several child
s.

Example:

<
相关标签:
14条回答
  • 2020-12-07 10:41

    I know this is a jQuery related question, but I believe someone might get here expecting a pure Javascript solution. So, if you were trying to do this using js, you could use the innerHTML property and set it to an empty string.

    document.getElementById('masterdiv').innerHTML = '';
    
    0 讨论(0)
  • 2020-12-07 10:42

    You can use .empty() function to clear all the child elements

     $(document).ready(function () {
      $("#button").click(function () {
       //only the content inside of the element will be deleted
       $("#masterdiv").empty();
      });
     });
    

    To see the comparison between jquery .empty(), .hide(), .remove() and .detach() follow here http://www.voidtricks.com/jquery-empty-hide-remove-detach/

    0 讨论(0)
  • 2020-12-07 10:43
    $('#div_id').empty();
    

    or

    $('.div_class').empty();
    

    Works Fine to remove contents inside a div

    0 讨论(0)
  • 2020-12-07 10:44
    jQuery('#masterdiv div').html('');
    
    0 讨论(0)
  • 2020-12-07 10:44

    try them if it help.

    $('.div_parent .div_child').empty();

    $('#div_parent #div_child').empty();

    0 讨论(0)
  • 2020-12-07 10:45
    $("#masterdiv div[id^='childdiv']").each(function(el){$(el).empty();});
    

    or

    $("#masterdiv").find("div[id^='childdiv']").each(function(el){$(el).empty();});
    
    0 讨论(0)
提交回复
热议问题