📄 dom.js
字号:
dojo.dom.createDocument = function(){ // summary // cross-browser implementation of creating an XML document object. var doc = null; var _document = dojo.doc(); if(!dj_undef("ActiveXObject")){ var prefixes = [ "MSXML2", "Microsoft", "MSXML", "MSXML3" ]; for(var i = 0; i<prefixes.length; i++){ try{ doc = new ActiveXObject(prefixes[i]+".XMLDOM"); }catch(e){ /* squelch */ }; if(doc){ break; } } }else if((_document.implementation)&& (_document.implementation.createDocument)){ doc = _document.implementation.createDocument("", "", null); } return doc; // DOMDocument}dojo.dom.createDocumentFromText = function(/* string */str, /* string? */mimetype){ // summary // attempts to create a Document object based on optional mime-type, using str as the contents of the document if(!mimetype){ mimetype = "text/xml"; } if(!dj_undef("DOMParser")){ var parser = new DOMParser(); return parser.parseFromString(str, mimetype); // DOMDocument }else if(!dj_undef("ActiveXObject")){ var domDoc = dojo.dom.createDocument(); if(domDoc){ domDoc.async = false; domDoc.loadXML(str); return domDoc; // DOMDocument }else{ dojo.debug("toXml didn't work?"); } /* }else if((dojo.render.html.capable)&&(dojo.render.html.safari)){ // FIXME: this doesn't appear to work! // from: http://web-graphics.com/mtarchive/001606.php // var xml = '<?xml version="1.0"?>'+str; var mtype = "text/xml"; var xml = '<?xml version="1.0"?>'+str; var url = "data:"+mtype+";charset=utf-8,"+encodeURIComponent(xml); var req = new XMLHttpRequest(); req.open("GET", url, false); req.overrideMimeType(mtype); req.send(null); return req.responseXML; */ }else{ var _document = dojo.doc(); if(_document.createElement){ // FIXME: this may change all tags to uppercase! var tmp = _document.createElement("xml"); tmp.innerHTML = str; if(_document.implementation && _document.implementation.createDocument) { var xmlDoc = _document.implementation.createDocument("foo", "", null); for(var i = 0; i < tmp.childNodes.length; i++) { xmlDoc.importNode(tmp.childNodes.item(i), true); } return xmlDoc; // DOMDocument } // FIXME: probably not a good idea to have to return an HTML fragment // FIXME: the tmp.doc.firstChild is as tested from IE, so it may not // work that way across the board return ((tmp.document)&& (tmp.document.firstChild ? tmp.document.firstChild : tmp)); // DOMDocument } } return null;}dojo.dom.prependChild = function(/* Element */node, /* Element */parent) { // summary // prepends node to parent's children nodes if(parent.firstChild) { parent.insertBefore(node, parent.firstChild); } else { parent.appendChild(node); } return true; // boolean}dojo.dom.insertBefore = function(/* Node */node, /* Node */ref, /* boolean? */force){ // summary // Try to insert node before ref if (force != true && (node === ref || node.nextSibling === ref)){ return false; } var parent = ref.parentNode; parent.insertBefore(node, ref); return true; // boolean}dojo.dom.insertAfter = function(/* Node */node, /* Node */ref, /* boolean? */force){ // summary // Try to insert node after ref var pn = ref.parentNode; if(ref == pn.lastChild){ if((force != true)&&(node === ref)){ return false; // boolean } pn.appendChild(node); }else{ return this.insertBefore(node, ref.nextSibling, force); // boolean } return true; // boolean}dojo.dom.insertAtPosition = function(/* Node */node, /* Node */ref, /* string */position){ // summary // attempt to insert node in relation to ref based on position if((!node)||(!ref)||(!position)){ return false; // boolean } switch(position.toLowerCase()){ case "before": return dojo.dom.insertBefore(node, ref); // boolean case "after": return dojo.dom.insertAfter(node, ref); // boolean case "first": if(ref.firstChild){ return dojo.dom.insertBefore(node, ref.firstChild); // boolean }else{ ref.appendChild(node); return true; // boolean } break; default: // aka: last ref.appendChild(node); return true; // boolean }}dojo.dom.insertAtIndex = function(/* Node */node, /* Element */containingNode, /* number */insertionIndex){ // summary // insert node into child nodes nodelist of containingNode at insertionIndex. var siblingNodes = containingNode.childNodes; // if there aren't any kids yet, just add it to the beginning if (!siblingNodes.length){ containingNode.appendChild(node); return true; // boolean } // otherwise we need to walk the childNodes // and find our spot var after = null; for(var i=0; i<siblingNodes.length; i++){ var sibling_index = siblingNodes.item(i)["getAttribute"] ? parseInt(siblingNodes.item(i).getAttribute("dojoinsertionindex")) : -1; if (sibling_index < insertionIndex){ after = siblingNodes.item(i); } } if (after){ // add it after the node in {after} return dojo.dom.insertAfter(node, after); // boolean }else{ // add it to the start return dojo.dom.insertBefore(node, siblingNodes.item(0)); // boolean }} dojo.dom.textContent = function(/* Node */node, /* string */text){ // summary // implementation of the DOM Level 3 attribute; scan node for text if (arguments.length>1) { var _document = dojo.doc(); dojo.dom.replaceChildren(node, _document.createTextNode(text)); return text; // string } else { if(node.textContent != undefined){ //FF 1.5 return node.textContent; // string } var _result = ""; if (node == null) { return _result; } for (var i = 0; i < node.childNodes.length; i++) { switch (node.childNodes[i].nodeType) { case 1: // ELEMENT_NODE case 5: // ENTITY_REFERENCE_NODE _result += dojo.dom.textContent(node.childNodes[i]); break; case 3: // TEXT_NODE case 2: // ATTRIBUTE_NODE case 4: // CDATA_SECTION_NODE _result += node.childNodes[i].nodeValue; break; default: break; } } return _result; // string }}dojo.dom.hasParent = function (/* Node */node) { // summary // returns whether or not node is a child of another node. return node && node.parentNode && dojo.dom.isNode(node.parentNode); // boolean}/** * Examples: * * myFooNode = <foo /> * isTag(myFooNode, "foo"); // returns "foo" * isTag(myFooNode, "bar"); // returns "" * isTag(myFooNode, "FOO"); // returns "" * isTag(myFooNode, "hey", "foo", "bar"); // returns "foo"**/dojo.dom.isTag = function(/* Node */node /* ... */) { // summary // determines if node has any of the provided tag names and returns the tag name that matches, empty string otherwise. if(node && node.tagName) { for(var i=1; i<arguments.length; i++){ if(node.tagName==String(arguments[i])){ return String(arguments[i]); // string } } } return ""; // string}dojo.dom.setAttributeNS = function(/* Element */elem, /* string */namespaceURI, /* string */attrName, /* string */attrValue){ // summary // implementation of DOM2 setAttributeNS that works cross browser. if(elem == null || ((elem == undefined)&&(typeof elem == "undefined"))){ dojo.raise("No element given to dojo.dom.setAttributeNS"); } if(!((elem.setAttributeNS == undefined)&&(typeof elem.setAttributeNS == "undefined"))){ // w3c elem.setAttributeNS(namespaceURI, attrName, attrValue); }else{ // IE // get a root XML document var ownerDoc = elem.ownerDocument; var attribute = ownerDoc.createNode( 2, // node type attrName, namespaceURI ); // set value attribute.nodeValue = attrValue; // attach to element elem.setAttributeNode(attribute); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -