📄 dom.js
字号:
* @method inDocument * @param {String | HTMLElement} el The element to search for * @return {Boolean} Whether or not the element is present in the current document */ inDocument: function(el) { var f = function(el) { // safari contains fails for body so crawl up if (isSafari) { while (el = el.parentNode) { // note assignment if (el == document.documentElement) { return true; } } return false; } return this.isAncestor(document.documentElement, el); }; return Y.Dom.batch(el, f, Y.Dom, true); }, /** * Returns a array of HTMLElements that pass the test applied by supplied boolean method. * For optimized performance, include a tag and/or root node when possible. * @method getElementsBy * @param {Function} method - A boolean method for testing elements which receives the element as its only argument. * @param {String} tag (optional) The tag name of the elements being collected * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point * @param {Function} apply (optional) A function to apply to each element when found * @return {Array} Array of HTMLElements */ getElementsBy: function(method, tag, root, apply) { tag = tag || '*'; root = (root) ? Y.Dom.get(root) : null || document; if (!root) { return []; } var nodes = [], elements = root.getElementsByTagName(tag); for (var i = 0, len = elements.length; i < len; ++i) { if ( method(elements[i]) ) { nodes[nodes.length] = elements[i]; if (apply) { apply(elements[i]); } } } return nodes; }, /** * Returns an the method(s) return value(s). * 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 methods */ batch: function(el, method, o, override) { el = (el && el.tagName) ? el : Y.Dom.get(el); // skip get() when possible if (!el || !method) { return false; } var scope = (override) ? o : window; if (el.tagName || (!el.item && !el.slice)) { // not a collection or array, just run the method 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()); 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()); 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 } 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 } 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) ) { return node; } } 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) { 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) { 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) { 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) { 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) { 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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -