Opposite of append in jquery

后端 未结 6 1045
难免孤独
难免孤独 2021-01-31 07:05

I use .append to add to a div

$(this).append(\'
  • test
\');

how can I search for a

相关标签:
6条回答
  • 2021-01-31 07:17

    The opposite of .append() is .prepend().

    From the jQuery documentation for prepend…

    The .prepend() method inserts the specified content as the first child of each element in the jQuery collection (To insert it as the last child, use .append()).

    I realize this doesn’t answer the OP’s specific case. But it does answer the question heading. :) And it’s the first hit on Google for “jquery opposite append”.

    0 讨论(0)
  • 2021-01-31 07:19

    You could use remove(). More information on jQuery remove().

    $(this).children("ul").remove();
    

    Note that this will remove all ul elements that are children.

    0 讨论(0)
  • 2021-01-31 07:21

    What you also should consider, is keeping a reference to the created element, then you can easily remove it specificly:

       var newUL = $('<ul><li>test</li></ul>');
       $(this).append(newUL);
    
       // Later ...
    
       newUL.remove();
    
    0 讨论(0)
  • 2021-01-31 07:28

    Opposite up is children(), but opposite in position is prepend(). Here a very good tutorial.

    0 讨论(0)
  • 2021-01-31 07:33

    Use the remove() method:

    $(this).children("ul").remove();
    
    0 讨论(0)
  • 2021-01-31 07:34

    just had the same problem and ive come across this - which actually does the trick for me:

    // $("#the_div").contents().remove();
    // or short: 
    $("#the_div").empty();
    $("#the_div").append("HTML goes in here...");
    
    0 讨论(0)
提交回复
热议问题