Home > Web Front-end > JS Tutorial > A small problem with javascript parameters_javascript skills

A small problem with javascript parameters_javascript skills

WBOY
Release: 2016-05-16 19:06:04
Original
1072 people have browsed it

以下是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               xpos =10
       }
       if (xpos > 480){
              xpos-=10
       }
       if (ypos               ypos =10
       }
       if (ypos > 80){
              ypos-=10
       }
       layer.style.width = xpos   "px";
       layer.style.height = ypos   "px";
       movement = setTimeout("animation()",0);
}


以下是html


   

 

 

 

 



问题
如果使用参数layer1、layer2、layer3、layer4,则每次都不成功,提示'layername' is undefined
如果将javascript里的layername换成div的id值就能成功,但如果这样javascript程序就会很多
请问怎样才能正确的使用参数的形式?
解决方法:
movement = setTimeout("animation()",0)
这个地方没有给animation()传参数过去

而定义的animation()函数也没有接受参数,但是又用
var layer = document.getElementById(layername);
来接收layername 这个变量
错误地理解了闭包?想从 function show(layername) 这个函数接收参数???

定时器好像没有丝毫作用
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template