The $ symbol is a characteristic character representing a variable in php. It also has many functions in js. Generally, we use it to name a function name and get the id.
1. First, it can be used to represent variables,
such as variable var s='asdsd' or var $s='asdasd';
2. In regular expressions, it can match the ending
/sa$/.test(string)
to match string characters The sa in the string, such as string='125sa' matches, string='125sa21' does not match
the regular expression is very complicated, here is just a brief talk.
3. Due to the influence of prototype.js (a framework written by foreigners, which is used to encapsulate some commonly used functions for easy operation), many people now use $ to represent a function for finding objects,
$=function (id) { return (typeof (id)=='object')?id:document.getElementByIdx_x(id); };
In fact, it is a custom function, using $ is just simple, in fact, the use of other characters is the same,
f=function (id) { return (typeof (id)=='object')?id:document.getElementByIdx_x(id); };The parameter id can also be the id in the html document, such
as <div id='ss'></div>
obj=$('ss') is the referenced object with id='ss'
using the $() method. The
$() method is a document that is used too frequently in the DOM.
This is better than the methods in the DOM. You can pass in multiple ids as arguments and $() returns an Array object with all the required elements.
<HTML>
<HEAD>
<TITLE> Test Page </TITLE>
<script src="prototype-1.3.1.js"></script>
<script>
function test1()
{
var d = $('myDiv') ;
alert(d.innerHTML);
}
function test2()
{
var divs = $('myDiv','myOtherDiv');
for(i=0; i<divs.length; i++)
{
alert(divs[i]. innerHTML);
}
}
</script>
</HEAD>
<BODY>
<div id="myDiv">
<p>This is a paragraph</p>
</div>
<div id="myOtherDiv">
<p>This is another paragraph</p>
</div>
<input type="button" value=Test1 onclick="test1();"><br>
<input type="button" value=Test2 onclick="test2();"><br>
</BODY>
</HTML>
下面的这个getObject方法和$符号是一样的意思:
function getObject(elementId)
{
if (document.getElementByIdx_x)
{
return document.getElementByIdx_x(elementId);
}
else if(document.all)
{
return document.all[elementId];
}
else if(document.layers)
{
return document.layers[elementId];
}
}