【发布时间】:2013-02-04 08:12:03
【问题描述】:
<script type="text/javascript">
function centerItem(id,size)
{
var pad = (window.innerWidth - size)/2;
document.getElementById(id).style.marginLeft = pad+"px";
document.getElementById(id).style.marginRight = pad+"px";
}
function login()
{
document.getElementById('box').innerHTML="<img src="img/pleasewait.gif" />";
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('box').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","http://[lan ip]/Athena/lib/ajax/login.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("email="+email+"&pass="+pass);
}
</script>
那在我的<head> 部分,我用这个来称呼它。
<script type="text/javascript">centerItem('login',210);</script>
但是,我收到一条错误消息,提示“未捕获的 ReferenceError:未定义 centerItem (匿名函数)”
有什么想法吗?
【问题讨论】:
-
调用函数的
<script>标签在哪里?在<head>或<body>? -
另外,你应该这样调用你的函数:
centerItem('login',210); -
centerItem的参数列表中缺少逗号。 -
该函数位于 中,而调用位于之后的 中,带有 id。缺少逗号是因为第二个代码部分没有复制/粘贴,而是匆忙输入,就像我原来的那样。
标签: javascript html function centering