jQuery 的 .load()
方法通常用于从服务器加载数据并将其放入匹配的元素中。默认情况下,它会替换目标元素的内容,但通过一些技巧可以实现追加数据的效果。
在某些场景下,我们可能需要:
$('#target').load('data.html #content', function(response, status, xhr) {
if (status == "success") {
var newContent = $(response).find('#content').html();
$('#target').append(newContent);
}
});
$.get('data.html', function(data) {
var temp = $('<div>').html(data);
$('#target').append(temp.find('#content').html());
});
fetch('data.html')
.then(response => response.text())
.then(html => {
$('#target').append($(html).find('#content'));
});
如果项目允许使用现代JavaScript,可以考虑:
通过以上方法,您可以灵活地使用jQuery的.load()
功能来实现数据追加而非替换的效果。