First, Code parsing
1 var$newdiv 1 = $ (' <div id= "Object1"/> '),2Newdiv2 = document.createelement (' div '),3Existingdiv1 = document.getElementById (' foo '));4 5 6 varRxhtmltag =/< (?! Area|br|col|embed|hr|img|input|link|meta|param) (([\w:]+) [^>]*] \/>/gi;7 varWrapmap = {8 //Support:ie 99Option: [1, "<select multiple= ' multiple ' >", "</select>"],TenTHEAD: [1, "<table>", "</table>"], OneCol: [2, "<table><colgroup>", "</colgroup></table>"], ATR: [2, "<table><tbody>", "</tbody></table>"], -TD: [3, "<table><tbody><tr>", "</tr></tbody></table>"], -_default: [0, "", "" "] the }; - //Support:ie 9 -Wrapmap.optgroup =wrapmap.option; -Wrapmap.tbody = Wrapmap.tfoot = Wrapmap.colgroup = Wrapmap.caption =Wrapmap.thead; +wrapmap.th =wrapmap.td; - + A functionbuildfragment (elems, context) { at varElem, TMP, tag, wrap, contains, J, -Fragment =context.createdocumentfragment (), -nodes = [], -i = 0, -L =elems.length; - in //filter out different types of nodes - for(; i < L; i++) { toElem =Elems[i]; + - if(Elem | | elem = = 0) { the if(Jquery.type (elem) = = = "Object") { * //if it's a jquery object $ //if it is an ordinary element object plus [Elem]Panax Notoginseng //remove ele into the nodes array -Jquery.merge (nodes, Elem.nodetype?)[Elem]: elem); the //No HTML structure, is a text node +}Else if(!/<|&#?\w+;/. Test (Elem)) { A Nodes.push (Context.createtextnode (Elem)); the}Else { + //Create an element div as a container -TMP = TMP | | Fragment.appendchild (Context.createelement ("div")); $Tag = (/< ([\w:]+)/.exec (elem) | | ["", ""]) [1].tolowercase (); $ //ie trimleft the string and the rest is user input processing - //Many tags cannot be separated as child elements of a div - //Td,th,tr,tfoot,tbody and so on, need to add the tail theWrap = Wrapmap[tag] | |Wrapmap._default; -tmp.innerhtml = wrap[1] + elem.replace (rxhtmltag, "<$1></$2>") + wrap[2];Wuyi the //because Warp was packaged. - //need to find the correct element parent Wu //Navigate to the lowest element -j = wrap[0]; About while(j--) { $TMP =Tmp.lastchild; - } - //Support:qtwebkit - //Jquery.merge Because Push.apply (_, arraylike) throws A //Copy the node to the nodes array. + Jquery.merge (nodes, tmp.childnodes); the } - } $ } thei = 0; the while((Elem = nodes[i++])) { the //' <td> Hello </td> ' This statement can only be inserted <td> good </td>, and will not give the statement plus <table><tbody><tr> </tr></tbody></table> the fragment.appendchild (elem) - } in returnfragment; the } the About the the$ (' #test1 '). Click (function() { the"<table><tbody><tr><td> Network </td></tr></tbody></table>" +$ (' body '). Append ($newdiv 1, [Newdiv2, Existingdiv1, ' <td> net </td> ', ' text ', ' <script>alert (1) ']) - }) the Bayi$ (' #test2 '). Click (function() { the //6 types of data Structures the //no script execution is designed here. - varfragment = Buildfragment ([$newdiv 1, Newdiv2, Existingdiv1, ' <td> mu class net </td> ', ' text ', ' <script>alert (1) '], document) - Document.body.appendChild (fragment) the})
jquery source Code Analysis----Analog Core buildfragment