📄 dom.js
字号:
delta[1] = (style_pos == 'relative') ? 0 : el.offsetTop;
}
if (pos[0] !== null) { el.style.left = pos[0] - pageXY[0] + delta[0] + 'px'; }
if (pos[1] !== null) { el.style.top = pos[1] - pageXY[1] + delta[1] + 'px'; }
var newXY = this.getXY(el);
// if retry is true, try one more time if we miss
if (!noRetry && (newXY[0] != pos[0] || newXY[1] != pos[1]) ) {
this.setXY(el, pos, true);
}
};
util.Dom.batch(el, f, util.Dom, true);
},
/**
* Set the X position of an html element in page coordinates, regardless of how the element is positioned.
* The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @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 {Int} x to use as the X coordinate for the element(s).
*/
setX: function(el, x) {
util.Dom.setXY(el, [x, null]);
},
/**
* Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
* The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
* @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 {Int} x to use as the Y coordinate for the element(s).
*/
setY: function(el, y) {
util.Dom.setXY(el, [null, y]);
},
/**
* Returns the region position of the given element.
* The element must be part of the DOM tree to have a region (display:none or elements not appended return false).
* @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 {Region/Array} A Region or array of Region instances containing "top, left, bottom, right" member data.
*/
getRegion: function(el) {
var f = function(el) {
var region = new YAHOO.util.Region.getRegion(el);
return region;
};
return util.Dom.batch(el, f, util.Dom, true);
},
/**
* Returns the width of the client (viewport).
* Now using getViewportWidth. This interface left intact for back compat.
* @return {Int} The width of the viewable area of the page.
*/
getClientWidth: function() {
return util.Dom.getViewportWidth();
},
/**
* Returns the height of the client (viewport).
* Now using getViewportHeight. This interface left intact for back compat.
* @return {Int} The height of the viewable area of the page.
*/
getClientHeight: function() {
return util.Dom.getViewportHeight();
},
/**
* Returns a array of HTMLElements with the given class
* For optimized performance, include a tag and/or root node if possible
* @param {String} className The class name to match against
* @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
* @return {Array} An array of elements that have the given class name
*/
getElementsByClassName: function(className, tag, root) {
var method = function(el) { return util.Dom.hasClass(el, className) };
return util.Dom.getElementsBy(method, tag, root);
},
/**
* Determines whether an HTMLElement has the given className
* @param {String/HTMLElement/Array} el The element or collection to test
* @param {String} className the class name to search for
* @return {Boolean/Array} A boolean value or array of boolean values
*/
hasClass: function(el, className) {
var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
var f = function(el) {
return re.test(el['className']);
};
return util.Dom.batch(el, f, util.Dom, true);
},
/**
* Adds a class name to a given element or collection of elements
* @param {String/HTMLElement/Array} el The element or collection to add the class to
* @param {String} className the class name to add to the class attribute
*/
addClass: function(el, className) {
var f = function(el) {
if (this.hasClass(el, className)) { return; } // already present
el['className'] = [el['className'], className].join(' ');
};
util.Dom.batch(el, f, util.Dom, true);
},
/**
* Removes a class name from a given element or collection of elements
* @param {String/HTMLElement/Array} el The element or collection to remove the class from
* @param {String} className the class name to remove from the class attribute
*/
removeClass: function(el, className) {
var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)', 'g');
var f = function(el) {
if (!this.hasClass(el, className)) { return; } // not present
var c = el['className'];
el['className'] = c.replace(re, ' ');
if ( this.hasClass(el, className) ) { // in case of multiple adjacent
this.removeClass(el, className);
}
};
util.Dom.batch(el, f, util.Dom, true);
},
/**
* Replace a class with another class for a given element or collection of elements.
* If no oldClassName is present, the newClassName is simply added.
* @param {String/HTMLElement/Array} el The element or collection to remove the class from
* @param {String} oldClassName the class name to be replaced
* @param {String} newClassName the class name that will be replacing the old class name
*/
replaceClass: function(el, oldClassName, newClassName) {
if (oldClassName === newClassName) { // avoid infinite loop
return false;
};
var re = new RegExp('(?:^|\\s+)' + oldClassName + '(?:\\s+|$)', 'g');
var f = function(el) {
if ( !this.hasClass(el, oldClassName) ) {
this.addClass(el, newClassName); // just add it if nothing to replace
return; // note return
}
el['className'] = el['className'].replace(re, ' ' + newClassName + ' ');
if ( this.hasClass(el, oldClassName) ) { // in case of multiple adjacent
this.replaceClass(el, oldClassName, newClassName);
}
};
util.Dom.batch(el, f, util.Dom, true);
},
/**
* Generates a unique ID
* @param {String/HTMLElement/Array} el (optional) An optional element array of elements to add an ID to (no ID is added if one is already present)
* @param {String} prefix (optional) an optional prefix to use (defaults to "yui-gen")
* @return {String/Array} The generated ID, or array of generated IDs (or original ID if already present on an element)
*/
generateId: function(el, prefix) {
prefix = prefix || 'yui-gen';
el = el || {};
var f = function(el) {
if (el) {
el = util.Dom.get(el);
} else {
el = {}; // just generating ID in this case
}
if (!el.id) {
el.id = prefix + id_counter++;
} // dont override existing
return el.id;
};
return util.Dom.batch(el, f, util.Dom, true);
},
/**
* Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy
* @param {String/HTMLElement} haystack The possible ancestor
* @param {String/HTMLElement} needle The possible descendent
* @return {Boolean} Whether or not the haystack is an ancestor of needle
*/
isAncestor: function(haystack, needle) {
haystack = util.Dom.get(haystack);
if (!haystack || !needle) { return false; }
var f = function(needle) {
if (haystack.contains && !isSafari) { // safari "contains" is broken
return haystack.contains(needle);
}
else if ( haystack.compareDocumentPosition ) {
return !!(haystack.compareDocumentPosition(needle) & 16);
}
else { // loop up and test each parent
var parent = needle.parentNode;
while (parent) {
if (parent == haystack) {
return true;
}
else if (!parent.tagName || parent.tagName.toUpperCase() == 'HTML') {
return false;
}
parent = parent.parentNode;
}
return false;
}
};
return util.Dom.batch(needle, f, util.Dom, true);
},
/**
* Determines whether an HTMLElement is present in the current document
* @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) {
return this.isAncestor(document.documentElement, el);
};
return util.Dom.batch(el, f, util.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 if possible
* @param {Function} method A boolean method to test elements with
* @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
*/
getElementsBy: function(method, tag, root) {
tag = tag || '*';
root = util.Dom.get(root) || document;
var nodes = [];
var elements = root.getElementsByTagName(tag);
if ( !elements.length && (tag == '*' && root.all) ) {
elements = root.all; // IE < 6
}
for (var i = 0, len = elements.length; i < len; ++i)
{
if ( method(elements[i]) ) { nodes[nodes.length] = elements[i]; }
}
return nodes;
},
/**
* Returns an array of elements that have had the supplied method applied.
* The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) )
* @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 {Generic} (optional) o An optional arg that is passed to the supplied method
* @param {Boolean} (optional) override Whether or not to override the scope of "method" with "o"
* @return {HTMLElement/Array} The element(s) with the method applied
*/
batch: function(el, method, o, override) {
var id = el;
el = util.Dom.get(el);
var scope = (override) ? o : window;
if (!el || el.tagName || !el.length) { // is null or not a collection (tagName for SELECT and others that can be both an element and a collection)
if (!el) {
return false;
}
return method.call(scope, el, o);
}
var collection = [];
for (var i = 0, len = el.length; i < len; ++i) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -