Home >
Article > Web Front-end > In-depth understanding of javascript dynamic insertion technology_javascript skills
In-depth understanding of javascript dynamic insertion technology_javascript skills
WBOYOriginal
2016-05-16 17:15:591847browse
Recently, I found that all major libraries can use div.innerHTML=HTML fragments to generate node elements, and then insert them into various positions of the target element. This thing is actually insertAdjacentHTML, but IE's abominable innerHTML turns this advantage into a disadvantage. First, innerHTML will remove the blank spaces in certain positions. See the results of the run box below:
IE's InnerHtml By Situ Zhengmei & lt;/Title & GT; & LT; Script Type = "Text/Javascript" & GT; Window.onload = Function () { VAR DIV = D = D = D = D ocument.createelement ( "div"); div.innerHTML = "
Situ正美 " " " alert("|" div.innerHTML "|"); var c = div.childNodes; alert(c[i].nodeType); if(c[i].nodeType === 1){ alert(":: " c[i] .childNodes.length); >
Another nasty thing is that the innerHTML of the following elements is read-only in IE: col, colgroup, frameset, html, head, style, table, tbody, tfoot, thead, title and tr. In order to clean them up, Ext specially made an insertIntoTable. insertIntoTable is added using DOM's insertBefore and appendChild. The situation is basically the same as jQuery. However, jQuery completely relies on these two methods, and Ext also uses insertAdjacentHTML. In order to improve efficiency, all class libraries use document fragmentation invariably. The basic process is to extract nodes through div.innerHTML, then transfer them to document fragments, and then use insertBefore and appendChild to insert nodes. For Firefox, Ext also uses createContextualFragment to parse the text and insert it directly into its target location. Obviously, Ext is much faster than jQuery. However, jQuery inserts not only HTML fragments, but also various nodes and jQuery objects. Let’s review the jQuery workflow.
Copy code
The code is as follows:
append: function() { //Pass in the arguments object, true means special processing of the table, callback function return this.domManip(arguments, true, function(elem){ if (this.nodeType == 1) this.appendChild( elem ); }); }, domManip: function( args, table, callback ) { if ( this[0] ) {//If there is an element node var fragment = (this[0].ownerDocument || this[0]).createDocumentFragment(), //Note that three are passed in here parameters scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment ), first = fragment.firstChild;
if ( first ) for ( var i = 0, l = this.length; i < l; i ) callback.call( root(this[i], first), this.length > 1 || i > ; 0 ? fragment.cloneNode(true) : fragment );
if ( scripts ) jQuery.each( scripts, evalScript ); }
return this ;
function root( elem, cur ) { return table && jQuery.nodeName(elem, "table") && jQuery.nodeName(cur, "tr") ? (elem.getElementsByTagName ("tbody")[0] || elem.appendChild(elem.ownerDocument.createElement("tbody"))) : elem; } } //elems are arguments object, context is the document object, and fragment is the empty document fragment clean: function( elems, context, fragment ) { context = context || document;
// !context.createElement fails in IE with an error but returns typeof 'object' if ( typeof context.createElement === "undefined" ) //Make sure context is a document object context = context.ownerDocument || context[ 0] && context[0].ownerDocument || document;
// If a single string is passed in and it's a single tag // just do a createElement and skip the rest / /If there is only one tag in the document object, such as
//We may call it like this from outside $(this).append("
") //At this time Directly take out the element name inside it, create it with document.createElement("div") and put it into the array to return if ( !fragment && elems.length === 1 && typeof elems[0] === "string " ) { var match = /^<(w )s*/?>$/.exec(elems[0]); if ( match ) return [ context.createElement( match [1] ) ]; } //Use the innerHTML of a div to create nodes var ret = [], scripts = [], div = context.createElement("div"); //If we add $(this).append("
Table 1
", "
Table 1
", "
Table 1< ;/td>") //jQuery.each traverses the agents object according to its fourth branching method (no parameters, length), callback.call( value, i, value ) jQuery.each (elems, function(i, elem){//i is the index, elem is the element in the arguments object if ( typeof elem === "number" ) elem = '';
if ( !elem ) return;
// Convert html string into DOM nodes if ( typeof elem === "string" ) { // Fix "XHTML"- style tags in all browsers elem = elem.replace(/(<(w )[^>]*?)/>/g, function(all, front, tag){ return tag. match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ? >"; });
// Trim whitespace, otherwise indexOf won't work as expected var tags = elem.replace(/^\s+/, "").substring(0, 10).toLowerCase();
var wrap = // option or optgroup !tags.indexOf(" [ 1, "" ] ||