<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title></Head><Body><DivID= "D1"> <DivID= "D1_1"></Div> <DivID= "D1_2"></Div></Div><DivID= "D2">Hello<DivID= "D2_1">I'm a dog.</Div> <DivID= "D2_2"></Div></Div><Table><TR><P><TDID= "haha">haha</TD></P></TR></Table><Scripttype= "Text/javascript">varMylibs={first:function(elem) {returnElem&&(Elem.firstelementchild||elem.firstchild); }, Last:function(elem) {returnElem&&(Elem.lastelementchild||elem.lastchild); }, Prev:function(elem) {returnElem&&(elem.previouselementsibling||elem.previoussibling); }, Next:function(elem) {returnElem&&(elem.nextelementsibling||elem.nextsibling); }, before:function(newelement,targetelement) {returntargetelement&&Targetelement.parentnode?TargetElement.parentNode.insertBefore (newelement,targetelement):NULL; }, after:function(newelement,targetelement) {returntargetelement&& This. Last (targetelement)==targetelement? //if the last node is the target element, it is added directly. Because the default is the last This. Before (newelement,targetelement)://if it is not, it is inserted in front of the next sibling node of the target element. Which is behind the target element. This. Before (Newelement, This. Next (targetelement)); }, replace:function(elem,newelement,oldelement) {}, remove:function(elem) {returnElem.parentNode.removeChild (Elem); }, Empty:function(elem) {elem.innerhtml= "'; returnElem; }, Append:function(elem,newelement) {returnElem.appendchild (newelement); }, Text:function(elem,value) {returnvalue===undefined?elem.textcontent||Elem.innerHTML.replace (/<[^<]+>/G,"') : This. Append ( This. Empty (Elem), (Elem&&elem.ownerdocument||document). createTextNode (value)); }, HTML:function(elem,value) {if(Value===undefined&&Elem.nodetype=== 1) returnelem.innerhtml; if(typeofvalue=== 'string' && /< (?: Script|style|link)/i.test (value)) {Value=Value.replace (/< (?! Area|br|col|embed|hr|img|input|link|meta|param) (([\w:]+) [^>]*] \/>/gi,'<$1></$2>'); Elem.innerhtml=value; } if(elem) { This. Append ( This. Empty (Elem), value); } } /*append ()-insert content at the end of the selected element prepend ()-insert content at the beginning of the selected element after ()-insert content after the selected element before ()-in Remove ()-delete the selected element (and its child elements) empty ()-*/};vardom1=document.getElementById ('D1'), Dom2=document.getElementById ('D2');vartmp=Document.createelement ('Div'); Console.log (Mylibs.after (TMP,DOM2));</Script></Body></HTML>
Self-encapsulated Operation Dom Method