Bypass A Lot Of if... else (actually defining the tocamel and getstyle functions) in the header first.-The browser implementation is not uniform, causing such a problem. You can talk about it later. Code .
Next we will look at the treasures in the Yahoo. util. Dom class. At present, the idea has been gradually split. I have seen a function.
// Basically, it can be considered as a revision of document. getelementbyid
Get: function (EL ){
// If it is already an htmlelement, it will be returned directly
If (El & (El. nodetype | El. Item )){
Return El;
}
// If it is a string, the element with this ID is returned.
If (Yahoo. Lang. isstring (EL) |! El ){
Return document. getelementbyid (EL );
}
// It looks like an array and calls itself cyclically to obtain the 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 well written. Frankly speaking, the loop body in the above Code will be written without thinking about it.
For (VAR I = 0, Len = El. length; I <Len; ++ I ){
C [C. Length] = Document. getelementbyid (El [I]);
} Although it can also work normally, the previous judgment is meaningless.
Continue to take a look at the internal mechanism of getelementsbyclassname. For details about calling getelementsbyclassname, see the Yui document.
Getelementsbyclassname: function (classname, Tag, root, apply ){
// Obtain tag tags. The default value is all ("*").
Tag = tag | '*';
// Specify the node name
Root = (Root )? Y. Dom. Get (Root): NULL | document;
If (! Root ){
Return [];
}
// Initialize node Information
VaR nodes = [],
Elements = root. getelementsbytagname (TAG ),
Re = getclassregex (classname );
// Filter out nodes that do not comply with the rules
For (VAR I = 0, Len = elements. length; I <Len; ++ I ){
If (Re. Test (elements [I]. classname )){
// You must be wondering why nodes. length is used instead of I
// Carefully consider: ^)
Nodes [nodes. Length] = elements [I];
// Execute the callback function
If (apply ){
Apply. Call (elements [I], elements [I]);
}
}
}
Return nodes;
}, Textbook Dom node acquisition and filtering, initialization data and operation data are very rigorous and formal, and Yui Code gives me a bit of "security ". Similarly, let's use the getelementsby function. The corresponding code is as follows:
Getelementsby: function (method, Tag, root, apply ){
// Same as the preceding function, omitted
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 ){
// Judge node attributes based on the return value of the custom function
If (method (elements [I]) {
Nodes [nodes. Length] = elements [I];
If (apply ){
Apply (elements [I]);
}
}
}
Return nodes;
}, OK. We will be here today.