1. Get the Content compatibility function
/** One: Get content compatibility function * SetText (obj, str) * Idea: * 1, first judge the browser; * 2, if it is IE browser, use innertext; * 3, if it is non-IE browser, use textcontent; * Parameter said Ming: * 1, if it is a parameter, this function will be used to get the content * 2, if it is two parameters, this function will be used to set the content **/functiongetText (obj, str) {//determine if it is IE browser, if obj.innertext = = ture, then is IE browser, otherwise non-IE browser if(obj.innertext) {//determines whether or not to pass in STR, if passed in, assigns the passed parameter to the object, and if not passed in, returns the object's contents directly . if(str) {Obj.innertext=str; } Else { returnObj.innertext; } } Else { if(str) {obj.textcontent=str; } Else { returnobj.textcontent; } }}
2, get the compatibility function of the style;
/************************************************************************************************************ * * Two: Get the compatibility function of the style * GetStyle (obj, attr); * Idea: * 1, first judge the browser, * 2, if it is IE browser, use obj.currentstyle[attr]; * 3, if it is a non-ie browser, use Document.defaultView.getComputedStyle (obj, null) [attr]; * Parameter Description: * Parameter 1: Is the object to get the style; * Parameter 2: The style to be obtained from the object, to be passed in as a string; * */functionGetStyle (obj, attr) {//Judging is not IE browser, if Obj.currentstyle = = True, the description is IE browser, otherwise non IE browser if(obj.currentstyle) {returnObj.currentstyle[attr]; } Else { returnDocument.defaultView.getComputedStyle (obj,NULL) [attr]; }};
3. Gets the collection of child elements of the specified element and the meaningful text, by default the node that gets the element;
/************************************************************************************************************ * * Three: Gets the collection of child elements of the specified element and meaningful text, by default the node that gets the element; * Getchild (obj, type); * Idea: * Get all the child elements of obj * pick: Obj.nodetype = = 1 Description is a collection of elements, otherwise it is a collection of text elements * Parameter Description: * Parameter 1: the specified element; * Parameter 2: Specify the type of get element; * true: Get only The node of the element; * false: Gets the node of the element and the meaningful text node; **/functiongetchild (obj, type) {varType = Type = = undefined?true:false, arr= []; varChild =Obj.childnodes; if(type) {//type==true| | Undefined the time of execution; for(vari = 0; i < child.length; i++) { if(Child[i].nodetype = = 1) {Arr.push (child[i]); }; }; returnarr; } Else { for(vari = 0; i < child.length; i++) { //Replace. (); Replace blank: str= "a b C"-------str=["a", "B", "C"] if(Child[i].nodetype = = 1 | | (Child[i].nodetype = = 3 && child[i].nodevalue.replace (/^\s+|\s+$/g, "")! = "") {Arr.push (child[i]); }; }; returnarr; };};
4. Get the first child element
/**/function getfirstchild (obj ) {return getchild (obj) [0 ];};
5. Get the Last child element
/**/function getlastchild (obj ) {var length = getchild ( obj). length; return getchild (obj) [length-1];}
6. Get arbitrary child elements
/** *function getrandomchild (obj, num ) {return getchild (obj) [Num-1];}
7. Get the next sibling node of the target object, if there is a next sibling node, if not, return false;
/************************************************************************************************************ * Seven: Get the next sibling node of the target object, if there is a next sibling node * getNext (obj, type); * Idea: * 1, determine if there is a next sibling node, if not, return false, if any, continue to proceed; * 2, the interpretation of next is an interesting text node or element node; * 3. Update next to assign value to next. Continue looking for the next sibling node; * 4, determine if Next is a null value, if NULL, return FALSE, if not empty, proceed to the second step; * Parameter Description: * 1, obj: Specified object; * 2, type true: Ignore text, is also the default When type is false, the text cannot be ignored;*/functiongetNext (obj, type) {varType = Type = = undefined?true: type; varNext =obj.nextsibling; Checknext (next); if(type) {//Ignore text //when next is a comment node or a text node, continue to loop down; while(Next.nodetype = = 3 | | next.nodetype = = 8) {Next=next.nextsibling; Checknext (next); } returnNext; } Else { //cannot ignore text //when the next type is a comment or text, continue to cycle down; while(Next.nodetype = = 8 | | (Next.nodetype = = 3 && next.nodeValue.replace (/^\s+|\s+$/g, ')! = ") ) {Next=next.nextsibling; Checknext (next); } } //returns false encapsulation into method functionChecknext (nextobj) {if(Nextobj = =NULL) { return false; } }}
View Code
7. Insert an element into the front element
/* *********************************************************************************************************** * Seven: Inserts an element into the front element * Beforechild (obj, ele) * Idea: * 1, get the first child element FirstChild * 2, parent element. InsertBefore (the element to insert, FirstChild); * Parameter Description: * 1, obj: parent element; * */function beforechild (obj, ele) {var First = getfirstchild (obj); Obj.insertbefore (Ele, first);};
8. Insert a child element after an element
About JS's function