简体   繁体   中英

How to use .append on a jQuery .load() function

我试图附加被调用的数据,而不是替换它,但是我不确定为什么.append无法正常工作。

      $('.incoming3').load('t3.html #test3').append;

Well first of all .append() is a function - you need to use parentheses after it, and supply it with a parameter telling it what to append. Second, you can't just change the way .load() works by chaining .append() onto the end of it.

Try this instead:

$.get("t3.html", function(data) {
    $('.incoming3').append($(data).filter("#test3"));
});

That is, use the $.get() method to retrieve the data, wrap the data in a jQuery object and use the .filter() method to extract the bit you need, and append that bit to your element.

$('.incoming3').append($('<div>').load('t3.html #test3'));

or

$.get('t3.html', function(data) {
    $('.incoming3').append($(data).find('#test3'));
});

load() method by design replaces the html of the selector. You will need to create your own filtering AJX method.

$.get( 't3.html', function(response){
   var contentDesired=$(response).find( '#test3')/* or filter instead of find if no body tag wrapping html*/
   $('.incoming3').append( contentDesired)

})

This is virtually identical to one solution I gave you on same situation in your last question for same issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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