A small javascript Parameter Problem

Source: Internet
Author: User

Below is 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
<Div id = "">
<Ul>
<Li id = "myfriends"> <a href = "#" onmouseover = "show ('layer1');"> my friends </a> </li>
<Li id = "mydocument"> <a href = "#" onmouseover = "show ('layer2');"> my documents </a> </li>
<Li id = "myalbum"> <a href = "#" onmouseover = "show ('layer3 ');"> my album </a> </li>
<Li id = "myarticles"> <a href = "#" onmouseover = "show ('layer4');"> my article </a> </li>
</Ul>
</Div>
<Div id = "layer1" class = "clearfloat"> </div>
<Div id = "layer2" class = "clearfloat"> </div>
<Div id = "layer3" class = "clearfloat"> </div>
<Div id = "layer4" class = "clearfloat"> </div>


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 div, the javascript program will
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.