简体   繁体   English

AJAX请求,获取具有相同类的多个链接的href属性

[英]AJAX request, getting href attribute for multiple links with same class

I'm writing a piece of jQuery that says if .ajaxLink is clicked, then take it's URL and apply it to the ajax call, like so: 我正在编写一段jQuery,说如果单击.ajaxLink,则使用它的URL并将其应用于ajax调用,如下所示:

$(".ajaxLink").click(function (e) {
    e.preventDefault();
    removePreviousData(); // removes any div that might be loaded in #content
    jQuery.ajax({
        type: 'GET',
        url: $(this).attr('href'),
        success: function (data, textStatus) {
            $(data).appendTo('#content');
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {},
        complete: function (XMLHttpRequest, textStatus) {
            loadedData() // applies some relative animations for loaded content
        }
    });
    return false;
});

This works fine for the first link that I click on, but since I have multiple links on the same page that use this class, when I click on the second link, it just loads the same data. 这对于我单击的第一个链接很好用,但是由于我在同一页面上有多个使用此类的链接,因此当我单击第二个链接时,它只会加载相同的数据。 How do I get it to apply to each .ajaxLink ( assuming .each(), but can't get that to run ). 我如何才能将其应用于每个.ajaxLink(假设使用.each(),但无法使其运行)。

Thanks! 谢谢!

If you want #content to show only the content of the href of the link clicked you'll have to replace #content 's content not append to it. 如果您希望#content仅显示所单击链接的href的内容,则必须替换#content的内容,而不是附加到该内容。

    success: function (data, textStatus) {
        $('#content').html(data);
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM