YUI read code Diary of Yahoo.util.dom-part.1_yui. Ext Related
Source: Internet
Author: User
Get around the head a lot of if...else (in fact, define Tocamel and GetStyle two functions)-because the browser's implementation is not uniform, causing such a cumbersome situation, you can talk about the code back.
Below we look at the treasures in the YAHOO.util.Dom class. Now that the thought has gradually split, I see a function that says a function.
It's basically a document.getElementById version.
Get:function (EL) {
If it's already htmlelement, then go straight back.
if (El && (El.nodetype | | el.item)) {
Return el;
}
If it is a string, then the Element with this ID is returned
if (YAHOO.lang.isString (EL) | |!el) {
Return document.getElementById (EL);
}
Looks like a number of groups, loops call itself, gets eelement
if (el.length!== undefined) {
var c = [];
for (var i = 0, len = el.length i < len; ++i) {
C[c.length] = Y.dom.get (El[i]);
}
return C;
}
Return el;
This code is very subtle. Frankly, the loop body in the above code, if not without thinking, would be written
for (var i = 0, len = el.length i < len; ++i) {
C[c.length] = document.getElementById (El[i]);
Although it works, the previous judgment loses its meaning.
Go on, and now look at the internal mechanism of getelementsbyclassname. Detailed call on Getelementsbyclassname, you can see the YUI document.
Getelementsbyclassname:function (ClassName, tag, root, apply) {
Get Tag label, default to All ("*")
Tag = Tag | | '*';
Designation of the following section
root = (root)? Y.dom.get (root): null | | Document
if (!root) {
return [];
}
Initializing node information
var nodes = [],
elements = root.getelementsbytagname (tag),
Re = Getclassregex (className);
Filter out nodes that don't conform to the rules
for (var i = 0, len = elements.length i < len; ++i) {
if (Re.test (Elements[i].classname)) {
You must be wondering why you use nodes.length instead of I
Careful consideration of: ^)
Nodes[nodes.length] = elements[i];
Executing a callback function
if (apply) {
Apply.call (Elements[i], elements[i]);
}
}
}
return nodes;
, the Textbook DOM node acquisition and filtering, initialization data and operational data are very rigorous and formal, YUI code let me have a bit of "security." Similarly, another Getelementsby function, the corresponding code is as follows
Getelementsby:function (method, tag, root, apply) {
The same as the above function, slightly
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) {
Determining the properties of a node based on the return value of a custom function
if (method (Elements[i])) {
Nodes[nodes.length] = elements[i];
if (apply) {
Apply (Elements[i]);
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service