簡體   English   中英

AJAX請求,獲取具有相同類的多個鏈接的href屬性

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

我正在編寫一段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;
});

這對於我單擊的第一個鏈接很好用,但是由於我在同一頁面上有多個使用此類的鏈接,因此當我單擊第二個鏈接時,它只會加載相同的數據。 我如何才能將其應用於每個.ajaxLink(假設使用.each(),但無法使其運行)。

謝謝!

如果您希望#content僅顯示所單擊鏈接的href的內容,則必須替換#content的內容,而不是附加到該內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM