Comparison between empty and html ('') in jQuery

Source: Internet
Author: User

HTML ()

return value: String
Overview:
Gets the HTML content of the first matching element. This function is not available for XML documents. But can be used for XHTML documents.

Empty ()

return value: JQuery
Overview
Deletes all the child nodes in the matching collection of elements.


The HTML () method returns the current contents of the selected element if the parameter is not set.
HTML ("") clears the current contents of the selected element.
The empty () method removes all content from the selected element, including all text and child nodes.


Example

Empty: function ()  {        var elem,    
         i = 0;         for  ( ;  (elem = this[i])  !=  null; i++ )  {            //  remove element nodes and prevent memory leaks     
        if  ( elem.nodeType === 1 )  {                                  //Cycle clears data                  jquery.cleandata ( 
GetAll ( elem, false )  );             }              //  removal of child             while   ( elem.firstChild )  {           
     elem.removechild ( elem.firstChild );             }              // if this is a select, ensure that  it displays empty  (#12336)              // support: ie<9              if  ( elem.options && jquery.nodename ( elem,  "select"  )  )  {                elem.options.length = 0;             }       
  }         return this;     },

In the code, you first erase all the data, so what does data contain?

The GetAll method finds all descendant elements. The GetAll code for jquery is as follows:

var strundefined = typeof undefined;
Function getall ( context, tag )  {    var elems, elem,         i = 0,          found = typeof context.getElementsByTagName !== strundefined ?  Context.getelementsbytagname ( tag | |   "*"  ) &NBSP:             typeof  Context.queryselectorall !== strundefined ? context.queryselectorall ( tag | |
  "*"  ) &NBSP:             undefined;     if  ( !found )  {        for   ( found = [], elems = context.childnodes | |  context;  (Elem = elems[i])  != null; i++ )  {            if  (  !tag | |  jquery.nodename ( elem, tag )  )  {       
         found.push ( elem );             } else {                 jquery.merge ( found, 
GetAll ( elem, tag )  );             }        &NBSP;&NBSP}     }     return tag === undefined | |
 tag && jquery.nodename ( context, tag )  ?         jquery.merge ( [ context ], found )  :
        found; } getAll (Document.body,false);// htmlcollection array



A collection of getall to be fetched, cleandata

Removeevent the event, freeing the memory (the jquery-bound event is saved in data), and the code below will find the list of events we bind. Expando is the identity of each object in the page that does not repeat jquery.

expando: "JQuery" + (version + Math.random ()). Replace (/\d/g, ""),

$ (' body '). On (' click ', Function () {

alert (' This is body ');

Console.log ($.cache[$ (' body ') [0][$.expando]]);

Delete Internalkey (object ID), push ID to Deletedids

Simply empty, the first loop is to remove the bindings for descendant elements, clear jquery to this DOM cache, and then loop Removefirstchild.

and the HTML ('), is simply violent setting innerHTML = ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.