Document. getelementbyid

Source: Internet
Author: User

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/

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.