📄 style.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.html.style");dojo.require("dojo.html.common");dojo.require("dojo.uri.Uri");dojo.html.getClass = function(/* HTMLElement */node){ // summary // Returns the string value of the list of CSS classes currently assigned directly // to the node in question. Returns an empty string if no class attribute is found; node = dojo.byId(node); if(!node){ return ""; } var cs = ""; if(node.className){ cs = node.className; }else if(dojo.html.hasAttribute(node, "class")){ cs = dojo.html.getAttribute(node, "class"); } return cs.replace(/^\s+|\s+$/g, ""); // string}dojo.html.getClasses = function(/* HTMLElement */node) { // summary // Returns an array of CSS classes currently assigned directly to the node in question. // Returns an empty array if no classes are found; var c = dojo.html.getClass(node); return (c == "") ? [] : c.split(/\s+/g); // array}dojo.html.hasClass = function(/* HTMLElement */node, /* string */classname){ // summary // Returns whether or not the specified classname is a portion of the // class list currently applied to the node. Does not cover cascaded // styles, only classes directly applied to the node. return (new RegExp('(^|\\s+)'+classname+'(\\s+|$)')).test(dojo.html.getClass(node)) // boolean}dojo.html.prependClass = function(/* HTMLElement */node, /* string */classStr){ // summary // Adds the specified class to the beginning of the class list on the // passed node. This gives the specified class the highest precidence // when style cascading is calculated for the node. Returns true or // false; indicating success or failure of the operation, respectively. classStr += " " + dojo.html.getClass(node); return dojo.html.setClass(node, classStr); // boolean}dojo.html.addClass = function(/* HTMLElement */node, /* string */classStr){ // summary // Adds the specified class to the end of the class list on the // passed &node;. Returns &true; or &false; indicating success or failure. if (dojo.html.hasClass(node, classStr)) { return false; } classStr = (dojo.html.getClass(node) + " " + classStr).replace(/^\s+|\s+$/g,""); return dojo.html.setClass(node, classStr); // boolean}dojo.html.setClass = function(/* HTMLElement */node, /* string */classStr){ // summary // Clobbers the existing list of classes for the node, replacing it with // the list given in the 2nd argument. Returns true or false // indicating success or failure. node = dojo.byId(node); var cs = new String(classStr); try{ if(typeof node.className == "string"){ node.className = cs; }else if(node.setAttribute){ node.setAttribute("class", classStr); node.className = cs; }else{ return false; } }catch(e){ dojo.debug("dojo.html.setClass() failed", e); } return true;}dojo.html.removeClass = function(/* HTMLElement */node, /* string */classStr, /* boolean? */allowPartialMatches){ // summary // Removes the className from the node;. Returns true or false indicating success or failure. try{ if (!allowPartialMatches) { var newcs = dojo.html.getClass(node).replace(new RegExp('(^|\\s+)'+classStr+'(\\s+|$)'), "$1$2"); } else { var newcs = dojo.html.getClass(node).replace(classStr,''); } dojo.html.setClass(node, newcs); }catch(e){ dojo.debug("dojo.html.removeClass() failed", e); } return true; // boolean}dojo.html.replaceClass = function(/* HTMLElement */node, /* string */newClass, /* string */oldClass) { // summary // Replaces 'oldClass' and adds 'newClass' to node dojo.html.removeClass(node, oldClass); dojo.html.addClass(node, newClass);}// Enum type for getElementsByClass classMatchType arg:dojo.html.classMatchType = { ContainsAll : 0, // all of the classes are part of the node's class (default) ContainsAny : 1, // any of the classes are part of the node's class IsOnly : 2 // only all of the classes are part of the node's class}dojo.html.getElementsByClass = function( /* string */classStr, /* HTMLElement? */parent, /* string? */nodeType, /* integer? */classMatchType, /* boolean? */useNonXpath){ // summary // Returns an array of nodes for the given classStr, children of a // parent, and optionally of a certain nodeType // FIXME: temporarily set to false because of several dojo tickets related // to the xpath version not working consistently in firefox. useNonXpath = false; var _document = dojo.doc(); parent = dojo.byId(parent) || _document; var classes = classStr.split(/\s+/g); var nodes = []; if( classMatchType != 1 && classMatchType != 2 ) classMatchType = 0; // make it enum var reClass = new RegExp("(\\s|^)((" + classes.join(")|(") + "))(\\s|$)"); var srtLength = classes.join(" ").length; var candidateNodes = []; if(!useNonXpath && _document.evaluate) { // supports dom 3 xpath var xpath = ".//" + (nodeType || "*") + "[contains("; if(classMatchType != dojo.html.classMatchType.ContainsAny){ xpath += "concat(' ',@class,' '), ' " + classes.join(" ') and contains(concat(' ',@class,' '), ' ") + " ')"; if (classMatchType == 2) { xpath += " and string-length(@class)="+srtLength+"]"; }else{ xpath += "]"; } }else{ xpath += "concat(' ',@class,' '), ' " + classes.join(" ') or contains(concat(' ',@class,' '), ' ") + " ')]"; } var xpathResult = _document.evaluate(xpath, parent, null, XPathResult.ANY_TYPE, null); var result = xpathResult.iterateNext(); while(result){ try{ candidateNodes.push(result); result = xpathResult.iterateNext(); }catch(e){ break; } } return candidateNodes; // NodeList }else{ if(!nodeType){ nodeType = "*"; } candidateNodes = parent.getElementsByTagName(nodeType); var node, i = 0; outer: while(node = candidateNodes[i++]){ var nodeClasses = dojo.html.getClasses(node); if(nodeClasses.length == 0){ continue outer; } var matches = 0; for(var j = 0; j < nodeClasses.length; j++){ if(reClass.test(nodeClasses[j])){ if(classMatchType == dojo.html.classMatchType.ContainsAny){ nodes.push(node); continue outer; }else{ matches++; } }else{ if(classMatchType == dojo.html.classMatchType.IsOnly){ continue outer; } } } if(matches == classes.length){ if( (classMatchType == dojo.html.classMatchType.IsOnly)&& (matches == nodeClasses.length)){ nodes.push(node); }else if(classMatchType == dojo.html.classMatchType.ContainsAll){ nodes.push(node); } } } return nodes; // NodeList }}dojo.html.getElementsByClassName = dojo.html.getElementsByClass;dojo.html.toCamelCase = function(/* string */selector){ // summary // Translates a CSS selector string to a camel-cased one. var arr = selector.split('-'), cc = arr[0]; for(var i = 1; i < arr.length; i++) { cc += arr[i].charAt(0).toUpperCase() + arr[i].substring(1); } return cc; // string}dojo.html.toSelectorCase = function(/* string */selector){ // summary // Translates a camel cased string to a selector cased one. return selector.replace(/([A-Z])/g, "-$1" ).toLowerCase(); // string}dojo.html.getComputedStyle = function(/* HTMLElement */node, /* string */cssSelector, /* integer? */inValue){ // summary // Returns the computed style of cssSelector on node. node = dojo.byId(node); // cssSelector may actually be in camel case, so force selector version var cssSelector = dojo.html.toSelectorCase(cssSelector); var property = dojo.html.toCamelCase(cssSelector); if(!node || !node.style){ return inValue; } else if (document.defaultView && dojo.html.isDescendantOf(node, node.ownerDocument)){ // W3, gecko, KHTML try{ // mozilla segfaults when margin-* and node is removed from doc // FIXME: need to figure out a if there is quicker workaround var cs = document.defaultView.getComputedStyle(node, ""); if(cs){ return cs.getPropertyValue(cssSelector); // integer } }catch(e){ // reports are that Safari can throw an exception above if(node.style.getPropertyValue){ // W3 return node.style.getPropertyValue(cssSelector); // integer } else { return inValue; // integer } } } else if(node.currentStyle){ // IE return node.currentStyle[property]; // integer } if(node.style.getPropertyValue){ // W3 return node.style.getPropertyValue(cssSelector); // integer }else{ return inValue; // integer }}dojo.html.getStyleProperty = function(/* HTMLElement */node, /* string */cssSelector){ // summary // Returns the value of the passed style node = dojo.byId(node); return (node && node.style ? node.style[dojo.html.toCamelCase(cssSelector)] : undefined); // string}dojo.html.getStyle = function(/* HTMLElement */node, /* string */cssSelector){ // summary // Returns the computed value of the passed style var value = dojo.html.getStyleProperty(node, cssSelector); return (value ? value : dojo.html.getComputedStyle(node, cssSelector)); // string || integer}dojo.html.setStyle = function(/* HTMLElement */node, /* string */cssSelector, /* string */value){ // summary // Set the value of passed style on node node = dojo.byId(node); if(node && node.style){ var camelCased = dojo.html.toCamelCase(cssSelector); node.style[camelCased] = value; }}dojo.html.setStyleText = function (/* HTMLElement */target, /* string */text) { // summary // Try to set the entire cssText property of the passed target; equiv of setting style attribute. try { target.style.cssText = text; } catch (e) { target.setAttribute("style", text); }}dojo.html.copyStyle = function(/* HTMLElement */target, /* HTMLElement */source){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -