📄 dom-debug.js
字号:
/*Copyright (c) 2007, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.net/yui/license.txtversion: 2.3.0*//** * The dom module provides helper methods for manipulating Dom elements. * @module dom * */(function() { var Y = YAHOO.util, // internal shorthand getStyle, // for load time browser branching setStyle, // ditto id_counter = 0, // for use with generateId propertyCache = {}, // for faster hyphen converts reClassNameCache = {}; // cache regexes for className // brower detection var isOpera = YAHOO.env.ua.opera, isSafari = YAHOO.env.ua.webkit, isGecko = YAHOO.env.ua.gecko, isIE = YAHOO.env.ua.ie; // regex cache var patterns = { HYPHEN: /(-[a-z])/i, // to normalize get/setStyle ROOT_TAG: /^body|html$/i // body for quirks mode, html for standards }; var toCamel = function(property) { if ( !patterns.HYPHEN.test(property) ) { return property; // no hyphens } if (propertyCache[property]) { // already converted return propertyCache[property]; } var converted = property; while( patterns.HYPHEN.exec(converted) ) { converted = converted.replace(RegExp.$1, RegExp.$1.substr(1).toUpperCase()); } propertyCache[property] = converted; return converted; //return property.replace(/-([a-z])/gi, function(m0, m1) {return m1.toUpperCase()}) // cant use function as 2nd arg yet due to safari bug }; var getClassRegEx = function(className) { var re = reClassNameCache[className]; if (!re) { re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)'); reClassNameCache[className] = re; } return re; }; // branching at load instead of runtime if (document.defaultView && document.defaultView.getComputedStyle) { // W3C DOM method getStyle = function(el, property) { var value = null; if (property == 'float') { // fix reserved word property = 'cssFloat'; } var computed = document.defaultView.getComputedStyle(el, ''); if (computed) { // test computed before touching for safari value = computed[toCamel(property)]; } return el.style[property] || value; }; } else if (document.documentElement.currentStyle && isIE) { // IE method getStyle = function(el, property) { switch( toCamel(property) ) { case 'opacity' :// IE opacity uses filter var val = 100; try { // will error if no DXImageTransform val = el.filters['DXImageTransform.Microsoft.Alpha'].opacity; } catch(e) { try { // make sure its in the document val = el.filters('alpha').opacity; } catch(e) { YAHOO.log('getStyle: IE filter failed', 'error', 'Dom'); } } return val / 100; case 'float': // fix reserved word property = 'styleFloat'; // fall through default: // test currentStyle before touching var value = el.currentStyle ? el.currentStyle[property] : null; return ( el.style[property] || value ); } }; } else { // default to inline only getStyle = function(el, property) { return el.style[property]; }; } if (isIE) { setStyle = function(el, property, val) { switch (property) { case 'opacity': if ( YAHOO.lang.isString(el.style.filter) ) { // in case not appended el.style.filter = 'alpha(opacity=' + val * 100 + ')'; if (!el.currentStyle || !el.currentStyle.hasLayout) { el.style.zoom = 1; // when no layout or cant tell } } break; case 'float': property = 'styleFloat'; default: el.style[property] = val; } }; } else { setStyle = function(el, property, val) { if (property == 'float') { property = 'cssFloat'; } el.style[property] = val; }; } var testElement = function(node, method) { return node && node.nodeType == 1 && ( !method || method(node) ); }; /** * Provides helper methods for DOM elements. * @namespace YAHOO.util * @class Dom */ YAHOO.util.Dom = { /** * Returns an HTMLElement reference. * @method get * @param {String | HTMLElement |Array} el Accepts a string to use as an ID for getting a DOM reference, an actual DOM reference, or an Array of IDs and/or HTMLElements. * @return {HTMLElement | Array} A DOM reference to an HTML element or an array of HTMLElements. */ get: function(el) { if (!el || el.tagName || el.item) { // null, HTMLElement, or HTMLCollection return el; } if (YAHOO.lang.isString(el)) { // HTMLElement or null return document.getElementById(el); } if (el.splice) { // Array of HTMLElements/IDs var c = []; for (var i = 0, len = el.length; i < len; ++i) { c[c.length] = Y.Dom.get(el[i]); } return c; } return el; // some other object, just pass it back }, /** * Normalizes currentStyle and ComputedStyle. * @method getStyle * @param {String | HTMLElement |Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements. * @param {String} property The style property whose value is returned. * @return {String | Array} The current value of the style property for the element(s). */ getStyle: function(el, property) { property = toCamel(property); var f = function(element) { return getStyle(element, property); }; return Y.Dom.batch(el, f, Y.Dom, true); }, /** * Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers. * @method setStyle * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements. * @param {String} property The style property to be set. * @param {String} val The value to apply to the given property. */ setStyle: function(el, property, val) { property = toCamel(property); var f = function(element) { setStyle(element, property, val); YAHOO.log('setStyle setting ' + property + ' to ' + val, 'info', 'Dom'); }; Y.Dom.batch(el, f, Y.Dom, true); }, /** * Gets the current position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false). * @method getXY * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements * @return {Array} The XY position of the element(s) */ getXY: function(el) { var f = function(el) { // has to be part of document to have pageXY if ( (el.parentNode === null || el.offsetParent === null || this.getStyle(el, 'display') == 'none') && el != document.body) { YAHOO.log('getXY failed: element not available', 'error', 'Dom'); return false; } var parentNode = null; var pos = []; var box; var doc = el.ownerDocument; if (el.getBoundingClientRect) { // IE box = el.getBoundingClientRect(); return [box.left + Y.Dom.getDocumentScrollLeft(el.ownerDocument), box.top + Y.Dom.getDocumentScrollTop(el.ownerDocument)]; } else { // safari, opera, & gecko pos = [el.offsetLeft, el.offsetTop]; parentNode = el.offsetParent; // safari: if el is abs or any parent is abs, subtract body offsets var hasAbs = this.getStyle(el, 'position') == 'absolute'; if (parentNode != el) { while (parentNode) { pos[0] += parentNode.offsetLeft; pos[1] += parentNode.offsetTop; if (isSafari && !hasAbs && this.getStyle(parentNode,'position') == 'absolute' ) { hasAbs = true; // we need to offset if any parent is absolutely positioned } parentNode = parentNode.offsetParent; } } if (isSafari && hasAbs) { //safari doubles in this case pos[0] -= el.ownerDocument.body.offsetLeft; pos[1] -= el.ownerDocument.body.offsetTop; } } parentNode = el.parentNode; // account for any scrolled ancestors while ( parentNode.tagName && !patterns.ROOT_TAG.test(parentNode.tagName) ) { // work around opera inline/table scrollLeft/Top bug if (Y.Dom.getStyle(parentNode, 'display').search(/^inline|table-row.*$/i)) { pos[0] -= parentNode.scrollLeft; pos[1] -= parentNode.scrollTop; } parentNode = parentNode.parentNode; } YAHOO.log('getXY returning ' + pos, 'info', 'Dom'); return pos; }; return Y.Dom.batch(el, f, Y.Dom, true); }, /** * Gets the current X position of an element based on page coordinates. The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false). * @method getX * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements * @return {Number | Array} The X position of the element(s) */ getX: function(el) { var f = function(el) { return Y.Dom.getXY(el)[0]; }; return Y.Dom.batch(el, f, Y.Dom, true); }, /** * Gets the current Y position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false). * @method getY * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements * @return {Number | Array} The Y position of the element(s) */ getY: function(el) { var f = function(el) { return Y.Dom.getXY(el)[1]; }; return Y.Dom.batch(el, f, Y.Dom, true); }, /** * Set the position of an html element in page coordinates, regardless of how the element is positioned. * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false). * @method setXY * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements * @param {Array} pos Contains X & Y values for new position (coordinates are page-based)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -