Home >Web Front-end >JS Tutorial >$()JS tips_javascript tips

$()JS tips_javascript tips

WBOY
WBOYOriginal
2016-05-16 19:11:20910browse

When we write JavaScript, we will definitely use the document.getElementById() method frequently. It is easy to make mistakes in such a long string, and getElementById is case-sensitive.
In fact, one method advocated in prototype.js is to use the $() abbreviation. Through the following function, you can use $('id') to implement the document.getElementById('id') function. How about it? Have fun!

Copy code The code is as follows:

function $() 

var elements = new Array();
for (var i = 0; i < arguments.length; i )
{
var elements = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}

And what’s even better about this function is that if you want it to submit an array with multiple IDs, it will also return multiple Array of elements!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn