<a href=""> link wont work when loaded with javascript</a>

时间:2013-07-30 21:38:04

标签: javascript html hyperlink href

我有一个非常小的页面由一些JavaScript管理。当我将页面加载到浏览器时,表单(id = login_linker)之后的链接正常工作。

然而,当我将它加载到div中时,相同的链接将不起作用。有没有人有任何想法。

这是包含的页面正文。

<body>
  <p>Forgot your password?</>
  <form name="forgotpassform"  id="forgotpassform" onsubmit="return false;">
    <label for="email">Email: </label>
    <input id="email" type="text" class="searchbox" onfocus="emptyElement('status')" maxlength="35">
 <input type="button" style="margin-top: 15px; position:relative; left:50px;" id="forgotpassbtn" onclick="forgotpass()" value="Send me a new password">
     <br>

  </form>
     <span id="emailstatus"> </span>
     <span id="status"></span>
 <a href="#" id="login_linker" alt="show login">Log In</a>
</body>

javascript:

function forgotpass(){
    var e = _("email").value;
    var status = _("status");
    var forgotpassform = _("forgotpassform");
    if(e != ""){
        _("forgotpassbtn").style.display = "none";
        status.innerHTML = 'please wait ...';
        var ajax = ajaxObj("POST", "forgotpass.php");
        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {
                if(ajax.responseText == "no_user"){
                    status.innerHTML = 'There was no matching email in the system.';                    
                    _("forgotpassbtn").style.display = "block";
                }  else if(ajax.responseText =="email_not_sent"){
                    status.innerHTML = 'There was a problem sending your temporary password.';      
                } else {
                //status.innerHTML = ajax.responseText;
                forgotpassform.innerHTML = "You have sent a temporary password to your email address. Please check your email.";    
                }
            }
        }
        ajax.send("e="+e);
    } else {
        status.innerHTML = "Please enter your email address.";
    }
}

function emptyElement(x){
    _(x).innerHTML = "";
}

    function loadlogin(){
    $('#loginwindow').toggle(400);

    $('#loginwindow').load("forgotpass.php");
    $('#loginwindow').toggle(400);
}

$(document).ready(function(){
    $(document).on('click', '#login_linker', function(){
    alert('ok');
        showlogin();
    });
    });

function showlogin(){
$('#loginwindow').load("login.php");
$('#loginwindow').toggle(400);
        }

以下是加载忘记密码页面的脚本,即上面的HTML

function forgotpass(){
    $('#loginwindow').toggle(400);
    $('#loginwindow').load("forgotpass.php");
    $('#loginwindow').toggle(400);
}

1 个答案:

答案 0 :(得分:0)

我不知道你的代码如何使用js加载链接(如果你发布它会更好),但我想问题是你试图在文档准备就绪后绑定事件,并且那一刻,链接尚未加载。加载后绑定它。