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/"]