search

Home  >  Q&A  >  body text

javascript - About jquery's remove() method

I don’t quite understand here. Will it be deleted from the jquery object? If you want to delete it, how to delete it?

阿神阿神2726 days ago1094

reply all(1)I'll reply

  • 学习ing

    学习ing2017-07-05 11:08:29

    Regarding remove, we look at the source code like this: Excerpted from jquery2.x

    // keepData is for internal use only--do not document
        remove: function( selector, keepData ) {
            var elem,
                elems = selector ? jQuery.filter( selector, this ) : this,
                i = 0;
    
            for ( ; (elem = elems[i]) != null; i++ ) {
                if ( !keepData && elem.nodeType === 1 ) {
                    jQuery.cleanData( getAll( elem ) );
                }
    
                if ( elem.parentNode ) {
                    if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) {
                        setGlobalEval( getAll( elem, "script" ) );
                    }
                    elem.parentNode.removeChild( elem );
                }
            }
    
            return this;
        },

    Follow the example code $('p').remove()Combined with the source code, the practical one is the native removeChild

    https://developer.mozilla.org...

    And removeChild here is a description from MDN:

    The removed child node still exists in the memory, but it is not added to the DOM tree of the current document. Therefore, you can also add this node back to the document. Of course, the implementation needs to use another variable such as the above oldChild in the example is used to save the reference to this node. If the second method in the above syntax is used, that is, oldChild is not used to save the reference to this node, the removed node is considered to be useless in a short time. It will be recycled by memory management.

    The mark marked by the question owner will not be deleted from the jQuery object. Likewise, the question owner can find a jQuery dom on the page and have a look,
    For example

    var t = $('#test');
    t.remove();

    Here t is a jQuery object, containing the information of the dom that has been removed.

    As quoted from mdn above, if you do not use variables to save the deleted dom, then it is waiting to be recycled.

    Of course it may feel like the above, or t is used to save the dom information, and it is on t[0], then you can try delete(t[0])?

    = =I don’t know how to test whether the dom has been recycled. But basically it has been removed and not quoted. If the page DOM is not very complicated, it can be ignored for the time being. This is my opinion, please let me know if I’m wrong~

    reply
    0
  • Cancelreply