javascript $ refers to a class of methods defined by prototype. For example, "$("id")" gets the element whose page id is "id", and "$F("id")" gets Is the value of the element with the page id "id", which is read-only and not writable.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
The meaning of the $ symbol in Javascript
$ refers to a class of methods defined by prototype
$("id") gets the page The element with id "id"
$F("id") gets the value of the element with id "id" on the page. This is read-only and cannot be written
Universal methods
This package contains many predefined objects and universal methods. The obvious purpose of writing these methods is to save you a lot of repetitive coding and idioms.
Use the $() method
$() method is a convenience for the document.getElementById() method that is used too frequently in the DOM The abbreviation of , just 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. The following example will describe this to you.
<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>
Another benefit of this method is that you can pass in the id string or the element object itself, which makes it very useful when creating methods that can pass in any formal parameters.
Using the $F() method
The $F() method is another very popular shorthand. It can return the value of any input form control, such as a text box or drop-down box. This method can pass in the id of the element or the element itself.
<script> function test3() { alert( $F(’userName’) ); }</script><input type="text" id="userName" value="Joe Doe"><br> <input type="button" value=Test3 onclick="test3();"><br>
【Recommended learning: javascript advanced tutorial】
The above is the detailed content of What does javascript $ mean?. For more information, please follow other related articles on the PHP Chinese website!