1
    <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>

しかし、「Uncaught ReferenceError: centerItem が定義されていません (無名関数)」というエラーが表示されます。

何かご意見は?

4

1 に答える 1

0

document.getElementById('box').innerHTML="<img src="img/pleasewait.gif" />";
本当にあるべきです:
document.getElementById('box').innerHTML="<img src=\"img/pleasewait.gif\" />"

画像タグを作成するときは、二重引用符をエスケープする必要があります。

そして、選択した要素をキャッシュする必要があります。結果は次のようになります。

function centerItem(id, size) {
    var pad = (window.innerWidth - size)/2,
        elem = document.getElementById(id);
    elem.style.marginLeft = pad+"px";
    elem.style.marginRight = pad+"px";
}

function login() {
    var xmlhttp;
    var box = document.getElementById('box');
    box.innerHTML="<img src=\"img/pleasewait.gif\" />";

    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) {
            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);
}
于 2013-02-19T15:56:01.957 に答える