Javascript 在ajax中单击链接不起作用

Javascript 在ajax中单击链接不起作用,javascript,ajax,Javascript,Ajax,这是我的ajax和html代码。当我点击链接上的收件人国家时,它无法加载Recipient_Country.html,我在下面的GET方法中调用了它 <div id = "topbar"> <ul> <li> <a href="#recipient_country.html" onclick = "recipient_country()">Recipient Country</a>

这是我的ajax和html代码。当我点击链接上的收件人国家时,它无法加载Recipient_Country.html,我在下面的GET方法中调用了它

<div id = "topbar">
    <ul>
        <li>
            <a href="#recipient_country.html" onclick = "recipient_country()">Recipient Country</a>
        </li>
    </ul>
</div>
<div id = "content"></div>

function recipient_country(e) {

    (e || window.event).preventDefault();

    var con = document.getElementById('content'), xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function (e) { 

        if (xhr.readyState == 4 && xhr.status == 200) {
            con.innerHTML = xhr.responseText;
        }
    }

    xhr.open("GET", "recipient_country.html", true);
    xhr.setRequestHeader('Content-type', 'text/html');
    xhr.send();
}

职能接收国(e){ (e | | window.event).preventDefault(); var con=document.getElementById('content'),xhr=new-XMLHttpRequest(); xhr.onreadystatechange=函数(e){ 如果(xhr.readyState==4&&xhr.status==200){ con.innerHTML=xhr.responseText; } } 打开(“GET”,“recipient\u country.html”,true); setRequestHeader('Content-type','text/html'); xhr.send(); }
您忘记将参数
事件
传递给调用方法。由于参数不匹配,
recipient\u country(e)
方法永远不会被调用。单击时将
事件传递给函数

 <li><a href="#recipient_country.html" onclick = "recipient_country(event)">Recipient Country</a></li>

  • 既然您也标记了jQuery,我可以建议使用它的方法吗

    如果您计划使用jqueryversion>1.8,则需要根据需要稍微更改代码

    function recipient_country()
     {   
        $.ajax({
           url: "test.html",
           type:"get",   
        }).done(function(response) {
              $('#content').html(response);
        });
        return false;
     }
    

    这是一种旧的ajax方法。请使用jquery-ajax,它是最简单和容易实现的code@Sudharsan:jQuery并不是每个javascript问题的答案…@karlipppins-我不是说jQuery很容易学习和编写代码quickly@karlipoppins-是的,阿斯匹林在“查询方法”中回答:“你比较jquery或javascript的最佳方式是什么。@苏达桑:我不是想挑起战争,我只是说,在说“jquery!!”之前了解事情的运作方式很重要,特别是当问题不包含jquery时。您的意思是,他正在使用一种“旧的ajax方法”,直接操作XMLHttpRequest对象。。。但您认为jQuery的
    ajax()
    方法真正做了什么?如果您查看它的源代码,您会发现它只是问题所提到的同一
    XMLHttpRequest
    对象的包装。code.jquery.com/jquery latest.min.js“>这是有效的jquery库吗?我在单击该链接时出错,忽略具有[LenientThis]的get或属性集,因为“对象不正确。reps.js:494此页面上的脚本已禁用Web控制台日志API(Console.log、Console.info、Console.warn、Console.error)。ReferenceError:firebug中未定义google
    function recipient_country()
     {   
        $.ajax({
           url: "test.html",
           type:"get",   
        }).done(function(response) {
              $('#content').html(response);
        });
        return false;
     }