📄 dom-debug.js
字号:
/** * Runs the supplied method against each item in the Collection/Array. * The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ). * @method batch * @param {String | HTMLElement | Array} el (optional) An element or array of elements to apply the method to * @param {Function} method The method to apply to the element(s) * @param {Any} o (optional) An optional arg that is passed to the supplied method * @param {Boolean} override (optional) Whether or not to override the scope of "method" with "o" * @return {Any | Array} The return value(s) from the supplied method */ batch: function(el, method, o, override) { el = (el && (el.tagName || el.item)) ? el : Y.Dom.get(el); // skip get() when possible if (!el || !method) { YAHOO.log('batch failed: invalid arguments', 'error', 'Dom'); return false; } var scope = (override) ? o : window; if (el.tagName || el.length === undefined) { // element or not array-like return method.call(scope, el, o); } var collection = []; for (var i = 0, len = el.length; i < len; ++i) { collection[collection.length] = method.call(scope, el[i], o); } return collection; }, /** * Returns the height of the document. * @method getDocumentHeight * @return {Int} The height of the actual document (which includes the body and its margin). */ getDocumentHeight: function() { var scrollHeight = (document.compatMode != 'CSS1Compat') ? document.body.scrollHeight : document.documentElement.scrollHeight; var h = Math.max(scrollHeight, Y.Dom.getViewportHeight()); YAHOO.log('getDocumentHeight returning ' + h, 'info', 'Dom'); return h; }, /** * Returns the width of the document. * @method getDocumentWidth * @return {Int} The width of the actual document (which includes the body and its margin). */ getDocumentWidth: function() { var scrollWidth = (document.compatMode != 'CSS1Compat') ? document.body.scrollWidth : document.documentElement.scrollWidth; var w = Math.max(scrollWidth, Y.Dom.getViewportWidth()); YAHOO.log('getDocumentWidth returning ' + w, 'info', 'Dom'); return w; }, /** * Returns the current height of the viewport. * @method getViewportHeight * @return {Int} The height of the viewable area of the page (excludes scrollbars). */ getViewportHeight: function() { var height = self.innerHeight; // Safari, Opera var mode = document.compatMode; if ( (mode || isIE) && !isOpera ) { // IE, Gecko height = (mode == 'CSS1Compat') ? document.documentElement.clientHeight : // Standards document.body.clientHeight; // Quirks } YAHOO.log('getViewportHeight returning ' + height, 'info', 'Dom'); return height; }, /** * Returns the current width of the viewport. * @method getViewportWidth * @return {Int} The width of the viewable area of the page (excludes scrollbars). */ getViewportWidth: function() { var width = self.innerWidth; // Safari var mode = document.compatMode; if (mode || isIE) { // IE, Gecko, Opera width = (mode == 'CSS1Compat') ? document.documentElement.clientWidth : // Standards document.body.clientWidth; // Quirks } YAHOO.log('getViewportWidth returning ' + width, 'info', 'Dom'); return width; }, /** * Returns the nearest ancestor that passes the test applied by supplied boolean method. * For performance reasons, IDs are not accepted and argument validation omitted. * @method getAncestorBy * @param {HTMLElement} node The HTMLElement to use as the starting point * @param {Function} method - A boolean method for testing elements which receives the element as its only argument. * @return {Object} HTMLElement or null if not found */ getAncestorBy: function(node, method) { while ( (node = node.parentNode) ) { // NOTE: assignment if ( testElement(node, method) ) { YAHOO.log('getAncestorBy returning ' + node, 'info', 'Dom'); return node; } } YAHOO.log('getAncestorBy returning null (no ancestor passed test)', 'error', 'Dom'); return null; }, /** * Returns the nearest ancestor with the given className. * @method getAncestorByClassName * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point * @param {String} className * @return {Object} HTMLElement */ getAncestorByClassName: function(node, className) { node = Y.Dom.get(node); if (!node) { YAHOO.log('getAncestorByClassName failed: invalid node argument', 'error', 'Dom'); return null; } var method = function(el) { return Y.Dom.hasClass(el, className); }; return Y.Dom.getAncestorBy(node, method); }, /** * Returns the nearest ancestor with the given tagName. * @method getAncestorByTagName * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point * @param {String} tagName * @return {Object} HTMLElement */ getAncestorByTagName: function(node, tagName) { node = Y.Dom.get(node); if (!node) { YAHOO.log('getAncestorByTagName failed: invalid node argument', 'error', 'Dom'); return null; } var method = function(el) { return el.tagName && el.tagName.toUpperCase() == tagName.toUpperCase(); }; return Y.Dom.getAncestorBy(node, method); }, /** * Returns the previous sibling that is an HTMLElement. * For performance reasons, IDs are not accepted and argument validation omitted. * Returns the nearest HTMLElement sibling if no method provided. * @method getPreviousSiblingBy * @param {HTMLElement} node The HTMLElement to use as the starting point * @param {Function} method A boolean function used to test siblings * that receives the sibling node being tested as its only argument * @return {Object} HTMLElement or null if not found */ getPreviousSiblingBy: function(node, method) { while (node) { node = node.previousSibling; if ( testElement(node, method) ) { return node; } } return null; }, /** * Returns the previous sibling that is an HTMLElement * @method getPreviousSibling * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point * @return {Object} HTMLElement or null if not found */ getPreviousSibling: function(node) { node = Y.Dom.get(node); if (!node) { YAHOO.log('getPreviousSibling failed: invalid node argument', 'error', 'Dom'); return null; } return Y.Dom.getPreviousSiblingBy(node); }, /** * Returns the next HTMLElement sibling that passes the boolean method. * For performance reasons, IDs are not accepted and argument validation omitted. * Returns the nearest HTMLElement sibling if no method provided. * @method getNextSiblingBy * @param {HTMLElement} node The HTMLElement to use as the starting point * @param {Function} method A boolean function used to test siblings * that receives the sibling node being tested as its only argument * @return {Object} HTMLElement or null if not found */ getNextSiblingBy: function(node, method) { while (node) { node = node.nextSibling; if ( testElement(node, method) ) { return node; } } return null; }, /** * Returns the next sibling that is an HTMLElement * @method getNextSibling * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point * @return {Object} HTMLElement or null if not found */ getNextSibling: function(node) { node = Y.Dom.get(node); if (!node) { YAHOO.log('getNextSibling failed: invalid node argument', 'error', 'Dom'); return null; } return Y.Dom.getNextSiblingBy(node); }, /** * Returns the first HTMLElement child that passes the test method. * @method getFirstChildBy * @param {HTMLElement} node The HTMLElement to use as the starting point * @param {Function} method A boolean function used to test children * that receives the node being tested as its only argument * @return {Object} HTMLElement or null if not found */ getFirstChildBy: function(node, method) { var child = ( testElement(node.firstChild, method) ) ? node.firstChild : null; return child || Y.Dom.getNextSiblingBy(node.firstChild, method); }, /** * Returns the first HTMLElement child. * @method getFirstChild * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point * @return {Object} HTMLElement or null if not found */ getFirstChild: function(node, method) { node = Y.Dom.get(node); if (!node) { YAHOO.log('getFirstChild failed: invalid node argument', 'error', 'Dom'); return null; } return Y.Dom.getFirstChildBy(node); }, /** * Returns the last HTMLElement child that passes the test method. * @method getLastChildBy * @param {HTMLElement} node The HTMLElement to use as the starting point * @param {Function} method A boolean function used to test children * that receives the node being tested as its only argument * @return {Object} HTMLElement or null if not found */ getLastChildBy: function(node, method) { if (!node) { YAHOO.log('getLastChild failed: invalid node argument', 'error', 'Dom'); return null; } var child = ( testElement(node.lastChild, method) ) ? node.lastChild : null; return child || Y.Dom.getPreviousSiblingBy(node.lastChild, method); }, /** * Returns the last HTMLElement child. * @method getLastChild * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point * @return {Object} HTMLElement or null if not found */ getLastChild: function(node) { node = Y.Dom.get(node); return Y.Dom.getLastChildBy(node); }, /** * Returns an array of HTMLElement childNodes that pass the test method. * @method getChildrenBy * @param {HTMLElement} node The HTMLElement to start from * @param {Function} method A boolean function used to test children * that receives the node being tested as its only argument * @return {Array} A static array of HTMLElements */ getChildrenBy: function(node, method) { var child = Y.Dom.getFirstChildBy(node, method); var children = child ? [child] : []; Y.Dom.getNextSiblingBy(child, function(node) { if ( !method || method(node) ) { children[children.length] = node; } return false; // fail test to collect all children }); return children; }, /** * Returns an array of HTMLElement childNodes. * @method getChildren * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point * @return {Array} A static array of HTMLElements */ getChildren: function(node) { node = Y.Dom.get(node); if (!node) { YAHOO.log('getChildren failed: invalid node argument', 'error', 'Dom'); } return Y.Dom.getChildrenBy(node); }, /** * Returns the left scroll value of the document * @method getDocumentScrollLeft
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -