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.