javascript 高手看过来,这个js程序为什么不正确??
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script type="text/javascript">
function load( w,h,bkcolor)
{
obj=document.getElementById("inner");
obj.style.width=w;
obj.style.height=h;
obj.style.backgroundColor=bkcolor;
obj.style.cursor="pointer";
obj.onclick=function()
{
test(w,h);
}
}
function test(ww,hh )
{
alert( ww.toString()+" "+ hh.toString() );
var obj=document.getElementById("other");
obj.style.width= (ww*2).toString()+"px"; obj.style.height= (hh*2).toString()+"px";
obj.style.backgroundColor="#ff0000";
}
</script>
</head>
<body>
<div>
<div id="inner">test1</div>
<div id="other">test2</div>
</div>
<script language="javascript">
load("222px","111px","#333333");
</script>
</body>
</html>
这个程序的目的是 给inner动态添加一个click事件,使得它可以改变other的style,可是程序无法实现预想的功能,错在哪里?如何改?必须使用动态加载click!!
答案:错在ww和hh的值分别是"222px"、"111px",相当于(“222px” * 2)得出来就变成NaN了
将:
obj.style.width= (ww*2).toString()+"px"; obj.style.height= (hh*2).toString()+"px";
改成:
obj.style.width= (ww.substring(0,ww.length-2)*2)+"px";
obj.style.height= (hh.substring(0,hh.length-2)*2)+"px";
上一个:javascript无法获取对像的值!
下一个:html javascript 单步调试