A small question about javascript parameters _ javascript skills

Source: Internet
Author: User
A small javascript Parameter Problem: javascript

Function show (layername ){
If (! Document. getElementById) return false;
If (! Document. getElementById (layername) return false;
Var layer = document. getElementById (layername );
Layer. style. width = "0px ";
Layer. style. height = "0px ";
Layer. style. display = "block ";
Movement = setTimeout ("animation ()", 0)
}
Function animation (){
If (! Document. getElementById) return false;
If (! Document. getElementById (layername) return false;
Var layer = document. getElementById (layername );
Var xpos = parseInt (layer. style. width );
Var ypos = parseInt (layer. style. height );
If (xpos == 480 & ypos = 80 ){
Return true;
}
If (xpos <480 ){
Xpos + = 10
}
If (xpos & gt; 480 ){
Xpos-= 10
}
If (ypos <80 ){
Ypos + = 10
}
If (ypos> 80 ){
Ypos-= 10
}
Layer. style. width = xpos + "px ";
Layer. style. height = ypos + "px ";
Movement = setTimeout ("animation ()", 0 );
}


The following is html



  • My friends

  • My Documents

  • My album

  • My articles









Problem
If the parameters layer1, layer2, layer3, and layer4 are used, the operation fails. The prompt 'layername' is undefined.
If you replace layername in javascript with the id value of p, the operation will be successful.
How can we correctly use the parameter format?
Solution:
Movement = setTimeout ("animation ()", 0)
The parameter is not passed to animation ().

The defined animation () function does not accept parameters, but uses
Var layer = document. getElementById (layername );
To receive the variable layername.
Wrong understanding of closure? Want to receive parameters from function show (layername ???

The timer does not seem to have any effect.

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.