For example, use _$ (ID) instead of document.getElementById (ID).
If it's a pure substitution, you can use the following code
Copy Code code as follows:
function $ (ID) {document.getElementById (ID)}
Personal comparison recommends the definition of document.getElementById in prototype:
Copy Code code as follows:
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 use the following code to define:
Copy Code code as follows:
function $ (objectId) {
if (document.getElementById && document.getElementById (objectId)) {
return document.getElementById (OBJECTID);//The Consortium 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;
}
}
This writing can save a lot of characters for JS files, but also can avoid the error of input.
With a little note that $ is a custom value, you can write any non-javascript reserved keyword, such as I_d,myid, and so on, according to your preferences. If your page calls to the jquery library file, you can directly use the $ Instead of document.getElementById.