[JS] [jQuery] the difference between clearing elements html (""), innerHTML = "" And empty (): Regarding content leakage, jqueryinnerhtml
Differences between clearing elements html (""), innerHTML = "" And empty ()
I. Differences between clearing elements 1. Incorrect practices 1: $ ("# test" ).html (""); // The method may cause memory leakage
2. Error Method 2: $ ("# test") [0]. innerHTML = ""; // The method may cause memory leakage
3. correct practices:
//$("#test").empty();
Ii. Principles:
Using the innerHTML method in jquery to clear elements will inevitably lead to memory leakage. Because jquery does not directly use the browser event model for processing multiple events of the same element, it caches events by itself, traversal trigger and trigger:
- // Init the element's event structure
- Var events = jQuery. data (elem, "events") | jQuery. data (elem, "events ",{}),
- Handle = jQuery. data (elem, "handle") | jQuery. data (elem, "handle", function (){
- // Handle the second event of a trigger and when
- // An event is called after a page has unloaded
- Return typeof jQuery! = "Undefined "&&! JQuery. event. triggered?
- JQuery. event. handle. apply (arguments. callee. elem, arguments ):
- Undefined;
- });
The data method is used to associate some data to the element. The preceding event uses this mechanism to cache the event listener.
As you can see, directly innerHTML = "" without notifying jquery to clear the data associated with the elements to be deleted, this part of data will no longer be released, that is, memory leakage.
- Remove: function (selector ){
- If (! Selector | jQuery. filter (selector, [this]). length ){
- // Prevent memory leaks
- JQuery ("*", this). add ([this]). each (function (){
- JQuery. event. remove (this );
- JQuery. removeData (this );
- });
- If (this. parentNode)
- This. parentNode. removeChild (this );
- }
- },
- Empty: function (){
- // Remove element nodes and prevent memory leaks
- JQuery (this). children (). remove ();
- // Remove any remaining nodes
- While (this. firstChild)
- This. removeChild (this. firstChild );
- }
What is the difference between empty () and html ("") in jquery?
Empty (), html (""), and text ("") are the same When deleting the content in the matching element. JQuery source code has different implementations, but the effects are the same. You can test it.
Source code:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Script src = "../scripts/jquery-1.4.2.min.js" type = "text/javascript"> </script>
<Script>
$ (Function (){
$ ('# BtnEmpty'). click (function (){
$ ('# Aim'). empty ();
Alert ("empty ()");
});
$ ('# BtnHtml'). click (function (){
Certificate ('%aim'%.html ("");
Alert ('html ("")');
});
$ ('# Btntext'). click (function (){
$ ('# Aim'). text ("");
Alert ('text ("")');
});
});
</Script>
</Head>
<Body>
<Div id = "aim">
<Ul>
<Li> 111111111 </li>
<Li> 222222222 </li>
<Li> 333333333 </li>
<Li> 444444444 </li>
</Ul>
</Div>
<Button id = 'btnempty '> empt ...... remaining full text>
Why does jQuery no longer execute the following content after html code is output?
Alert ("ul" example .html ()
This is an error.
What do you mean by printing innerHtml of the <ul> node?
It should be: alert ($ ("ul" example .html ());
Also, do not mix jquery and js elements, which may cause problems.
$ Ul = $ ("ul). children () $ ul is a jquery element.
Therefore, it is best to change $ ul [I]. innerHTML to $ulw. I }.html ()
Also, since jquery is used thoroughly, do not use the for loop.
Try jquery's. each () and look back at the current for. Do you think the for is really inferior?
Good luck :)