使用<a onclick=""> in Javascript (AJAX)

时间:2016-04-20 17:36:00

标签: javascript jquery html css ajax

Hello i have an issue with this code that it's turning me mad (i'm a rookie in terms of JS language):

function procesarCambio(){
    if(obj.readyState == 4){
            if(obj.status == 200){
                document.getElementById("miDiv").innerHTML = obj.responseText;
                var persona;
                if(window.JSON)
                    persona = window.JSON.parse(obj.responseText);
                else
                    persona = eval( '(' + obj.responseText + ')' );
                if(window.sessionStorage){
                    sessionStorage.setItem("clave", persona.CLAVE);
                    sessionStorage.setItem("login", persona.LOGIN);
                    sessionStorage.setItem("nombre", persona.NOMBRE);
                    sessionStorage.setItem("email", persona.EMAIL);
                    sessionStorage.setItem("ultAcceso", persona.ULTIMO_ACCESO);
                    sessionStorage.setItem("foto", persona.FOTO);
                }
                //alert("Bienvenido" + sessionStorage.getItem("nombre")); //No tiene que ser un alert
                //setTimeout(window.location = "index.html", 0);
                document.getElementById("error").style.opacity = '0';
                document.getElementById("acierto").style.opacity = '1';
            }else{
            //alert("Nombre de usuario o contrase&ntilde;a incorrectos");//No tiene que ser un alert
            document.getElementById("acierto").style.opacity = '0';
            document.getElementById("error").style.opacity = '1';
            }
        }
}
function ocultar(){
    document.getElementById("error").style.opacity = '0';
    document.getElementById("login").focus();
    return false;
}

HTML:

<div id="acierto" class="popup acierto">
            <p>Welcome</p><a href="index.html"><img src="imagenes/x.png" height="32" width="32"></a>
            </div>
        <div id="error" class="popup error">
            <p>Error</p><a href="#" onclick="ocultar(); return false;"><img src="imagenes/x.png" height="32" width="32"></a>
        </div>

It handle the AJAX login request and then writes a message that can be both succesfull and error depending if the login gone right or not. It's all ok except for the fact that when i click on the X in the right corner of the success message, it calls the error function instead of go to index.html

1 个答案:

答案 0 :(得分:1)

使用style.display ='none'或'block'而不是style.opacity

            document.getElementById("error").style.display = 'none';
            document.getElementById("acierto").style.display = 'block';