Dollar sign in prototype frame $ usage Analysis _javascript Tips

Source: Internet
Author: User
Tags php explode

This example describes the dollar sign $ usage in the prototype framework. Share to everyone for your reference, specific as follows:

Prototype is an important tool for implementing object-oriented and is a good framework for JavaScript.
People with jquery know that there are $ dollar signs in jquery, and in prototype, there are $, what's the difference?

1, the use of $ () in prototype

Prototype writing $ ("test") or $$ ("#test"), he is equivalent to JS document.getElementById ("test");
Jqueryp Writing $ ("#test"), he is also equivalent to JS document.getElementById ("test");

2. $$ () Usage in prototype

jquery notation $ ("div") he is equivalent to document.getElementsByTagName (' div ')
If prototype also want to simply get all the DIV elements on the page, you can't write this.
Prototype $ ("div") he is equivalent to document.getElementById ("div")
Prototype writing $$ ("div") he is equivalent to document.getElementsByTagName (' div ')
Prototype writing $$ ("Input[value=tank]") Gets the label of the input box value of the page as tank, in fact, it's almost like jquery, just one more $ symbol.

3. $a () usage in prototype

The $A is primarily used to convert any set that can be used as an array (such as NodeList, the htmlcollection that many DOM methods return, or the arguments property of a function object) to a true array object. Here are four ways to hide div tags in a page

$A ($$ (' div ')). each (element.hide); 
$A ($$ (' div ')). Map (Element.extend). Invoke ("Hide"); 
Intellectually, the following two kinds of writing, I use jquery, also often with the 
$A ($$ (' div ')). each (function (name,index) { 
 name.style.display= ' none '; 
 alert (name.innerhtml); 
 }) 
 $A ($$ (' div ')). each (function (name,index) { 
 $ (name). Hide (); 
}) 

If the example appears TypeError:element.style is undefined {message= ' element.style is undefined ', more ...}, please raise your prototype version to a subordinate

4. $f () Usage in prototype

Personally feel that the use of $f is designed to be more convenient to take form data, but it is relatively single, because he can only through the ID to fetch

<input name= "name" value= "Tank" id= "name" >
$F ("name") the correct

<input name= "name" value= "Tank" >
$F (' name ') is incorrect, typeerror:element is null {message= ' element is null ', more ...}

5. $h () Usage in prototype

When you pass in a parameter to an image as a function, $H will enter a proprietary hash pair like a prototype. $H feel like a converter, to a change in the role of the situation

$H ({name: ' Tank ', sex:1,height: ' 170cm '}). ToArray ()
[["Name", "Tank"], ["Sex", 1], ["Height", "170cm"]]
$H ({ Name: ' Tank ', sex:1,height: ' 170cm '}. toquerystring ()
"name=tank&sex=1&height=170cm"

6, in Prototype "() usage

The $R function has exactly the same parameters as the original constructor: Start and end represent the lower and upper values (the type of the two arguments must be the same), and exclusive whether the top values (parameter end) are excluded. The upper bound value is not excluded by default.

A $R instance describes a series of values that follow a rule change, such as numbers, text, or other types that semantically support the derivation of adjacent values

To illustrate, the easiest thing to remember

$A ($R ("A", "Z", True). Join (', ')//Parameter True 
"A,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y" 
$A ($R ("a" , "Z"). Join (', ')//No 
"A,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z" 

$R inferred a "pair of pixels" from the rule, $A converts the pair to an array and converts the array to a string using the number.

$R (0). each (function (value) { 
 alert (value*value); 
}); 
The result is the same as above, but each explanation is different. 
$A ($R (0)). Each (function (value) { 
 alert (value*value); 
}); 

If you report such an error, TYPEERROR:VALUE.SUCC is not a function {message= "VALUE.SUCC isn't a function", more ...}, please upgrade

7. $w () Usage in prototype

$w will be delimited by a space string, converting the array, root PHP explode ("", $string); the function is the same, the pertinence is stronger.

$w ("AA BB"). Join (', ')

If you report such a mistake, Referenceerror: $w is isn't defined {message= "$w is isn't defined", more ...}, please upgrade

I hope this article will help you with JavaScript programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.