One of the four common functions used to write JavaScript code (self-written, very simple)

Source: Internet
Author: User

In the past, JavaScript was written using a framework, but basically all these four functions were used. However, when referencing a framework, the number of files to be referenced would be less than a dozen,

It is better to write it by yourself. If the write speed is less than 1 K, the loading speed of the webpage can be greatly improved. It is stated that the cainiao has written: No tests, and there may be bugs. You are welcome to raise them ,:

 

Code
// Returns an object by ID.
Function $ (ID ){
Return Document. getelementbyid (ID );
}
// Use the name attribute to obtain an array of Objects
Function $ Name (name ){
Return Document. getelementsbyname (name );
}
// Obtain an array of objects using the tag attribute.
Function $ Tagname (TAG ){
Return Document. getelementsbytagname (TAG );
}
// Formatted string STR: "Hello {0 }! ", ArgS {" liuliang "}; output:" Hello liuliang! "
Function Formatstring (STR, array ){
VaR Str = New String (STR );
VaR Formatstr = "" ;
For ( VaR I = 0 ; I < Array. length; I ++ ){
VaR Temp = " { " + I + " } " ;
VaR Index = Str. indexof (temp );

VaR Str1 = Str. substring ( 0 , Index - 1 );
Formatstr + = Str1 + Array [I];
Str2 = Str. substring (Index + 3 );
Str = Str2;
}
Return Formatstr;
}

 

InstanceCode:

 

< Script Type = " Text/JavaScript " Language = " Javascript " >
VaR Array = New Array ( " {1} " , " @ 2 @ " , " @ 3 @ " );
VaR S = Formatstring ( " You are {0}, who am I {1}, score {2} " ,[ " Replace one with me. " , " I am replacing 2 " , " /<> {}\ " \ "" ]);
Alert (s );
< / SCRIPT>

Formatstring (STR, array) the first parameter is a string such as: "Welcome to niuniu blog {0}. Please follow cainiao's group {1}", the second is the array ["http://www.cnblogs.com/niuniu", "http://space.cnblogs.com/group/niuniu/"]

Related Article

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.