Understanding domhelp. js

Source: Internet
Author: User
Domhelp. JS is an object that the browser encapsulates in domhelp for different Dom operations. The following methods are available: 1. firstsibling Description: obtains the return type of the first sibling node (element node) parameter: node (current node: the carriage return line break in the node HTML page is also a text node, tempobj. nodetype! = 1 is required to be an element node, while tempobj. nextsibling! = NULL is used to prevent the current node from being the first. 2. lastsibling Description: obtains the name of the last sibling node (element node). Parameter: node (current node). Return type: node 3. closestsibling description: get the node parameter of the sibling element closest to him: node (current node), direction (Direction 1-> next -1-> previous one)Return type: Like a node, you must determine whether it is an element node or whether there is a previous or next adjacent node. 4. cssjs Description: use JavaScript to replace, add, delete, and check parameters: A performs one operation on the properties of a four 'swap'-> replace 'add'-> Add 'delete'-> Delete 'check'-> check O operation on the Element Node object C1 operation the name of the CSS Class C2 is used only in replace. Return type: Multiple classes in undefinedcss can be separated by spaces to give an element several classes, such: <Div class = "a B cd"> </div> This method is used to add, delete, modify, and check case 'SWAp ': O. classname =! Domhelp.css JS ('check', O, C1 )? O. classname. Replace (C2, C1): O. classname. Replace (C1, C2); break; Case 'add': If (! Domhelp.css JS ('check', O, C1) {o. classname + = O. classname? ''+ C1: C1;} break; Case 'delete': var rep = O. classname. Match ('' + C1 )? ''+ C1: C1; O. classname = O. classname. replace (REP, ''); break; Case 'check': var found = false; var temparray = O. classname. split (''); For (VAR I = 0; I <temparray. length; I ++) {If (temparray [I] = C1) {found = true ;}} return found; 5, addevent Description: adds an event listening parameter to Elm (the object of the event listening to be added) evtype (the event type of the listener) FN (event processing method event listener) usecapture (Boolean capture event) Elm. addeventlistener this is prepared for Mozilla, Netscape, Firefox, Elm. attachevent is a unique method of IE, and all of them can be used at the end. 6. CreateLink Description: create a link. The link text is TXT. Link to the to parameter: To (where you want to link to). txt (link text) return value: <ahref = "to"> TXT </a>) 7. createtextelm Description: creates an element that contains the given text parameters: elm (created element), txt (included text) Return Value: formed object (<elm> TXT </elm>) 8, gettext Description: Get the text parameter in node: node (object) return value: the value of the text or false to get the content of the text. You must first find the text node, so you need to find it cyclically and confirm the tempobj. after nodetype = 3, you also need to consider the possible errors during the search process. At the same time, the carriage return and line feed in the HTML are considered as text nodes, exclude/^ \ s + $/and match all blank characters 9. settext Description: Set the text in the node to the TXT parameter: node (object) TXT (set to what text)Return Value: null or false10, gettarget Description: Get event target parameter: trigger event e return value: event Target e.tar get is used by Firefox and other standard browsers. in IE, event e is no longer valid, window is valid. event. Naturally, the event target is window. event. replace srcelement. The while loop is mainly for safari. in Safari, if the event target is a link, the link is not regarded as a target, but the text node in it is regarded as a target. 11. stopbubble Description: block event bubbles. Parameter: triggered event. Note: stoppropagation is not the IE method, but the preventdefault attribute in a window event called cancelbubble is not the IE method, but a returnvalue attribute 12. stopdefault Description: blocks processing parameters of default events: the event ewindow that is triggered. event & window. event. returnvalue is obviously for IE, while others are for other browsers. Block default events.
The source code is as follows:
DOMhelp={debugWindowId:'DOMhelpdebug',init:function(){if(!document.getElementByIdx_x || !document.createTextNode){return;}},lastSibling:function(node){var tempObj=node.parentNode.lastChild;while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){tempObj=tempObj.previousSibling;}return (tempObj.nodeType==1)?tempObj:false;},firstSibling:function(node){var tempObj=node.parentNode.firstChild;while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){tempObj=tempObj.nextSibling;}return (tempObj.nodeType==1)?tempObj:false;},getText:function(node){if(!node.hasChildNodes()){return false;}var reg=/^\s+$/;var tempObj=node.firstChild;while(tempObj.nodeType!=3 && tempObj.nextSibling!=null || reg.test(tempObj.nodeValue)){tempObj=tempObj.nextSibling;}return tempObj.nodeType==3?tempObj.nodeValue:false;},setText:function(node,txt){if(!node.hasChildNodes()){return false;}var reg=/^\s+$/;var tempObj=node.firstChild;while(tempObj.nodeType!=3 && tempObj.nextSibling!=null || reg.test(tempObj.nodeValue)){tempObj=tempObj.nextSibling;}if(tempObj.nodeType==3){tempObj.nodeValue=txt}else{return false;}},createLink:function(to,txt){var tempObj=document.createElement_x('a');tempObj.appendChild(document.createTextNode(txt));tempObj.setAttribute('href',to);return tempObj;},createTextElm:function(elm,txt){var tempObj=document.createElement_x(elm);tempObj.appendChild(document.createTextNode(txt));return tempObj;},closestSibling:function(node,direction){var tempObj;if(direction==-1 && node.previousSibling!=null){tempObj=node.previousSibling;while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){tempObj=tempObj.previousSibling;}}else if(direction==1 && node.nextSibling!=null){tempObj=node.nextSibling;while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){tempObj=tempObj.nextSibling;}}return tempObj.nodeType==1?tempObj:false;},initDebug:function(){if(DOMhelp.debug){DOMhelp.stopDebug();}DOMhelp.debug=document.createElement_x('div');DOMhelp.debug.setAttribute('id',DOMhelp.debugWindowId);document.body.insertBefore(DOMhelp.debug,document.body.firstChild);},setDebug:function(bug){if(!DOMhelp.debug){DOMhelp.initDebug();}DOMhelp.debug.innerHTML+=bug+'\n';},stopDebug:function(){if(DOMhelp.debug){DOMhelp.debug.parentNode.removeChild(DOMhelp.debug);DOMhelp.debug=null;}},getKey:function(e){if(window.event){     var key = window.event.keyCode;   } else if(e){     var key=e.keyCode;   }return key;},getTarget:function(e){var target = window.event ? window.event.srcElement : e ? e.target : null;if (!target){return false;}while(target.nodeType!=1 && target.nodeName.toLowerCase()!='body'){target=target.parentNode;}return target;},stopBubble:function(e){if(window.event && window.event.cancelBubble){window.event.cancelBubble = true;} if (e && e.stopPropagation){e.stopPropagation();}},stopDefault:function(e){if(window.event && window.event.returnValue){window.event.returnValue = false;} if (e && e.preventDefault){e.preventDefault();}},cancelClick:function(e){if (window.event){window.event.cancelBubble = true;window.event.returnValue = false;}if (e && e.stopPropagation && e.preventDefault){e.stopPropagation();e.preventDefault();}},addEvent: function(elm, evType, fn, useCapture){if (elm.addEventListener){elm.addEventListener(evType, fn, useCapture);return true;} else if (elm.attachEvent) {var r = elm.attachEvent('on' + evType, fn);return r;} else {elm['on' + evType] = fn;}},cssjs:function(a,o,c1,c2){switch (a){case 'swap':o.className=!DOMhelp.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);break;case 'add':if(!DOMhelp.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}break;case 'remove':var rep=o.className.match(' '+c1)?' '+c1:c1;o.className=o.className.replace(rep,'');break;case 'check':var found=false;var temparray=o.className.split(' ');for(var i=0;i<temparray.length;i++){if(temparray[i]==c1){found=true;}}return found;break;}},    safariClickFix:function(){      return false;    }}DOMhelp.addEvent(window, 'load', DOMhelp.init, false);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.