Home  >  Article  >  Web Front-end  >  What does $ mean in javascript

What does $ mean in javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-16 18:16:1820222browse

The meaning of "$" in JavaScript: 1. "$" can be used to represent variables, such as "var $s=wwd"; 2. In regular expressions, "$" represents the end of the matching string Position; 3. Use "$" to represent a function that searches for an object, instead of "document.getElementById".

What does $ mean in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer. The meaning of $ in

js is: The

$ symbol is a characteristic character representing variables in php. It also has many functions in js. Generally, we use it to name a function name and obtain the id. of.

1. First, it can be used to represent variables, such as variables var s='asdsd' or var $s='asdasd';

2. In regular expressions, it can match the end /sa$/.test(string) matches sa in the string string. For example, string='125sa' matches, string='125sa21' does not match. Regular expressions are very complicated, and here is just a brief explanation.

3. Due to the influence of prototype.js (a framework written by foreigners, used to encapsulate some commonly used functions for easy operation), many people now use $ to represent a function that finds an object, such as

$=function (id) {
  return (typeof (id)=='object')?id:document.getElementById(id);
};

In fact, it is a custom function. It is simple to use $. In fact, it is the same with other characters.

f=function (id) {
 return (typeof (id)=='object')?id:document.getElementById(id); 
};

The parameter id is the id in the html document, such as 5fb9cec6d72b058e3ea8fb1607e5cd1c16b28748ea4df4d9c2150843fecfba68 then obj=$('ss') refers to the object with id='ss' that uses the $() method

$() method is used too frequently in the DOM A convenient shorthand for the document.getElementById() method, like this DOM method, this method returns the element with the id passed in as the parameter.

Compared with the methods in DOM, this is better. You can pass multiple ids as arguments and $() returns an Array object with all required elements.

<HTML>
<HEAD>
<TITLE> Test Page </TITLE>
<script src="prototype-1.3.1.js"></script>
<script>
function test1()
{
var d = $(&#39;myDiv&#39;);
alert(d.innerHTML);
}
function test2()
{
var divs = $(&#39;myDiv&#39;,&#39;myOtherDiv&#39;);
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>

The above is the detailed content of What does $ mean in javascript. For more information, please follow other related articles on the PHP Chinese website!

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