Mr. Think is a person who likes conciseness, like poetry.Code. Do not like. Net code, because I often see the lengthy code segments generated by my colleagues after.
In thinking, javascrip is a very elegant language. Therefore, the code written to ta must also be elegant. For example_ $ (ID)ReplaceDocument. getelementbyid (ID).
For personal comparison, we recommend that you use prototype to define document. getelementbyid:
Function $ ()
{
VaR Elements = New Array () ;
For ( VaR I = 0 ; I < Arguments . Length ; I ++ )
{
VaR Element = Arguments [ I ] ;
If ( Typeof Element = ' String ' )
Element = Document . Getelementbyid ( Element ) ;
If ( Arguments . Length = 1 )
Return Element ;
Elements . Push ( Element ) ;
}
Return Elements ;
}
You can also define it using the following code:
Function $ ( Objectid ) {
If ( Document . Getelementbyid
&& Document . Getelementbyid ( Objectid )) {
Return Document . Getelementbyid ( Objectid ) ; //
W3C dom
} Else If ( Document . All && Document . All ( Objectid )) {
Return Document . All ( Objectid ) ; //
MSIE 4 dom
} Else If ( Document . Layers && Document . Layers [ Objectid ]) {
Return Document . Layers [ Objectid ] ; //
Nn 4 Dom .. Note: This won't find nested Layers
} Else {
Return False ;
}
}
Writing in this way can save a lot of characters for the JS file and avoid input errors.
There is a small note,$Is a custom value, you can write any non-JavaScript reserved keyword according to your preferences, suchI _d,Myid,
If jquery library files are called on your page, you can directly use$ReplaceDocument. getelementbyid.
personal blog published in mr. Think: http://mrthink.net/javascript-getbyid-simplewrite/