A small problem with JavaScript parameters _javascript tips
Source: Internet
Author: User
The following are 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 = && ypos = = 80) {
return true;
}
if (Xpos < 480) {
xpos+=10
}
if (Xpos > 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 an 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 Information </a></li>
<li id= "Myalbum" > <a href= "#" onmouseover= "Show (' Layer3 ');" > My albums </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 you use Parameters Layer1, Layer2, Layer3, layer4, it will not succeed each time, prompting ' layername ' is undefined
If you change the layername in JavaScript to a div ID, you can do it, but if you do, the JavaScript program will be a lot
How can I use the form of parameter correctly?
Workaround:
Movement = settimeout ("animation ()", 0)
This place does not give animation () Pass parameter past
The defined animation () function also does not accept parameters, but uses the
var layer = document.getElementById (layername);
To receive the LayerName variable.
Misunderstood the closure? Want to receive parameters from function show (LayerName)???
The timer doesn't seem to have the slightest effect
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.