📄 dom.js
字号:
/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml*/dojo.provide("dojo.dom");dojo.dom.ELEMENT_NODE = 1;dojo.dom.ATTRIBUTE_NODE = 2;dojo.dom.TEXT_NODE = 3;dojo.dom.CDATA_SECTION_NODE = 4;dojo.dom.ENTITY_REFERENCE_NODE = 5;dojo.dom.ENTITY_NODE = 6;dojo.dom.PROCESSING_INSTRUCTION_NODE = 7;dojo.dom.COMMENT_NODE = 8;dojo.dom.DOCUMENT_NODE = 9;dojo.dom.DOCUMENT_TYPE_NODE = 10;dojo.dom.DOCUMENT_FRAGMENT_NODE = 11;dojo.dom.NOTATION_NODE = 12; dojo.dom.dojoml = "http://www.dojotoolkit.org/2004/dojoml";/** * comprehensive list of XML namespaces**/dojo.dom.xmlns = { // summary // aliases for various common XML namespaces svg : "http://www.w3.org/2000/svg", smil : "http://www.w3.org/2001/SMIL20/", mml : "http://www.w3.org/1998/Math/MathML", cml : "http://www.xml-cml.org", xlink : "http://www.w3.org/1999/xlink", xhtml : "http://www.w3.org/1999/xhtml", xul : "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", xbl : "http://www.mozilla.org/xbl", fo : "http://www.w3.org/1999/XSL/Format", xsl : "http://www.w3.org/1999/XSL/Transform", xslt : "http://www.w3.org/1999/XSL/Transform", xi : "http://www.w3.org/2001/XInclude", xforms : "http://www.w3.org/2002/01/xforms", saxon : "http://icl.com/saxon", xalan : "http://xml.apache.org/xslt", xsd : "http://www.w3.org/2001/XMLSchema", dt: "http://www.w3.org/2001/XMLSchema-datatypes", xsi : "http://www.w3.org/2001/XMLSchema-instance", rdf : "http://www.w3.org/1999/02/22-rdf-syntax-ns#", rdfs : "http://www.w3.org/2000/01/rdf-schema#", dc : "http://purl.org/dc/elements/1.1/", dcq: "http://purl.org/dc/qualifiers/1.0", "soap-env" : "http://schemas.xmlsoap.org/soap/envelope/", wsdl : "http://schemas.xmlsoap.org/wsdl/", AdobeExtensions : "http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"};dojo.dom.isNode = function(/* object */wh){ // summary: // checks to see if wh is actually a node. if(typeof Element == "function") { try { return wh instanceof Element; // boolean } catch(e) {} } else { // best-guess return wh && !isNaN(wh.nodeType); // boolean }}dojo.dom.getUniqueId = function(){ // summary: // returns a unique string for use with any DOM element var _document = dojo.doc(); do { var id = "dj_unique_" + (++arguments.callee._idIncrement); }while(_document.getElementById(id)); return id; // string}dojo.dom.getUniqueId._idIncrement = 0;dojo.dom.firstElement = dojo.dom.getFirstChildElement = function(/* Element */parentNode, /* string? */tagName){ // summary: // returns the first child element matching tagName var node = parentNode.firstChild; while(node && node.nodeType != dojo.dom.ELEMENT_NODE){ node = node.nextSibling; } if(tagName && node && node.tagName && node.tagName.toLowerCase() != tagName.toLowerCase()) { node = dojo.dom.nextElement(node, tagName); } return node; // Element}dojo.dom.lastElement = dojo.dom.getLastChildElement = function(/* Element */parentNode, /* string? */tagName){ // summary: // returns the last child element matching tagName var node = parentNode.lastChild; while(node && node.nodeType != dojo.dom.ELEMENT_NODE) { node = node.previousSibling; } if(tagName && node && node.tagName && node.tagName.toLowerCase() != tagName.toLowerCase()) { node = dojo.dom.prevElement(node, tagName); } return node; // Element}dojo.dom.nextElement = dojo.dom.getNextSiblingElement = function(/* Node */node, /* string? */tagName){ // summary: // returns the next sibling element matching tagName if(!node) { return null; } do { node = node.nextSibling; } while(node && node.nodeType != dojo.dom.ELEMENT_NODE); if(node && tagName && tagName.toLowerCase() != node.tagName.toLowerCase()) { return dojo.dom.nextElement(node, tagName); } return node; // Element}dojo.dom.prevElement = dojo.dom.getPreviousSiblingElement = function(/* Node */node, /* string? */tagName){ // summary: // returns the previous sibling element matching tagName if(!node) { return null; } if(tagName) { tagName = tagName.toLowerCase(); } do { node = node.previousSibling; } while(node && node.nodeType != dojo.dom.ELEMENT_NODE); if(node && tagName && tagName.toLowerCase() != node.tagName.toLowerCase()) { return dojo.dom.prevElement(node, tagName); } return node; // Element}// TODO: hmph/*this.forEachChildTag = function(node, unaryFunc) { var child = this.getFirstChildTag(node); while(child) { if(unaryFunc(child) == "break") { break; } child = this.getNextSiblingTag(child); }}*/dojo.dom.moveChildren = function(/*Element*/srcNode, /*Element*/destNode, /*boolean?*/trim){ // summary: // Moves children from srcNode to destNode and returns the count of // children moved; will trim off text nodes if trim == true var count = 0; if(trim) { while(srcNode.hasChildNodes() && srcNode.firstChild.nodeType == dojo.dom.TEXT_NODE) { srcNode.removeChild(srcNode.firstChild); } while(srcNode.hasChildNodes() && srcNode.lastChild.nodeType == dojo.dom.TEXT_NODE) { srcNode.removeChild(srcNode.lastChild); } } while(srcNode.hasChildNodes()){ destNode.appendChild(srcNode.firstChild); count++; } return count; // number}dojo.dom.copyChildren = function(/*Element*/srcNode, /*Element*/destNode, /*boolean?*/trim){ // summary: // Copies children from srcNde to destNode and returns the count of // children copied; will trim off text nodes if trim == true var clonedNode = srcNode.cloneNode(true); return this.moveChildren(clonedNode, destNode, trim); // number}dojo.dom.replaceChildren = function(/*Element*/node, /*Node*/newChild){ // summary: // Removes all children of node and appends newChild. All the existing // children will be destroyed. // FIXME: what if newChild is an array-like object? var nodes = []; if(dojo.render.html.ie){ for(var i=0;i<node.childNodes.length;i++){ nodes.push(node.childNodes[i]); } } dojo.dom.removeChildren(node); node.appendChild(newChild); for(var i=0;i<nodes.length;i++){ dojo.dom.destroyNode(nodes[i]); }}dojo.dom.removeChildren = function(/*Element*/node){ // summary: // removes all children from node and returns the count of children removed. // The children nodes are not destroyed. Be sure to call destroyNode on them // after they are not used anymore. var count = node.childNodes.length; while(node.hasChildNodes()){ dojo.dom.removeNode(node.firstChild); } return count; // int}dojo.dom.replaceNode = function(/*Element*/node, /*Element*/newNode){ // summary: // replaces node with newNode and returns a reference to the removed node. // To prevent IE memory leak, call destroyNode on the returned node when // it is no longer needed. return node.parentNode.replaceChild(newNode, node); // Node}dojo.dom.destroyNode = function(/*Node*/node){ // summary: // destroy a node (it can not be used any more). For IE, this is the // right function to call to prevent memory leaks. While for other // browsers, this is identical to dojo.dom.removeNode if(node.parentNode){ node = dojo.dom.removeNode(node); } if(node.nodeType != 3){ // ingore TEXT_NODE if(dojo.evalObjPath("dojo.event.browser.clean", false)){ dojo.event.browser.clean(node); } if(dojo.render.html.ie){ node.outerHTML=''; //prevent ugly IE mem leak associated with Node.removeChild (ticket #1727) } }}dojo.dom.removeNode = function(/*Node*/node){ // summary: // if node has a parent, removes node from parent and returns a // reference to the removed child. // To prevent IE memory leak, call destroyNode on the returned node when // it is no longer needed. // node: // the node to remove from its parent. if(node && node.parentNode){ // return a ref to the removed child return node.parentNode.removeChild(node); //Node }}dojo.dom.getAncestors = function(/*Node*/node, /*function?*/filterFunction, /*boolean?*/returnFirstHit){ // summary: // returns all ancestors matching optional filterFunction; will return // only the first if returnFirstHit var ancestors = []; var isFunction = (filterFunction && (filterFunction instanceof Function || typeof filterFunction == "function")); while(node){ if(!isFunction || filterFunction(node)){ ancestors.push(node); } if(returnFirstHit && ancestors.length > 0){ return ancestors[0]; // Node } node = node.parentNode; } if(returnFirstHit){ return null; } return ancestors; // array}dojo.dom.getAncestorsByTag = function(/*Node*/node, /*String*/tag, /*boolean?*/returnFirstHit){ // summary: // returns all ancestors matching tag (as tagName), will only return // first one if returnFirstHit tag = tag.toLowerCase(); return dojo.dom.getAncestors(node, function(el){ return ((el.tagName)&&(el.tagName.toLowerCase() == tag)); }, returnFirstHit); // Node || array}dojo.dom.getFirstAncestorByTag = function(/*Node*/node, /*string*/tag){ // summary: // Returns first ancestor of node with tag tagName return dojo.dom.getAncestorsByTag(node, tag, true); // Node
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -