2

是否可以使用 jQuery.load并将加载的内容粘贴到变量中,以便我可以使用该变量并将其附加到其他内容?

4

5 回答 5

4

你可以,但这是不必要的。请改用该$.get方法直接访问响应。

$.get("foo.php",function(response){
    console.log(response);
});

如果你想两者都做(即将响应加载到 div 并使用返回的数据),你可以$.load类似地使用方法上的回调。

于 2012-11-16T11:29:51.337 回答
4

根据文档

.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] )

因此,您可以创建函数来保存您的响应:

$('#result').load('ajax/test.html', function(responseText, textStatus, request) {
    alert(responseText);
});

其他方法是使用$.get$.post方法:

$.get('ajax/test.html', function(data) {
    $('.result').html(data);
    alert(data);
});

$.post('ajax/test.html', function(data) {
    $('.result').html(data);
    alert(data);
});
于 2012-11-16T11:29:55.330 回答
2
$(selector).load(url);

只是以下的简写:

$.get(url, data, function(response) {
    $(selecton).replaceWith(response);
});
于 2012-11-16T11:30:44.593 回答
2

你可以在 AJAX 中尝试这样的事情

$.post(yourfile,function(response) {
     //div hidden with the html of your page
      $("#hiddendiv").html(response);
});

或使用获取

$.get(yourfile,function(response) {
     //div hidden with the html of your page
      $("#hiddendiv").html(response);
});
于 2012-11-16T11:31:01.870 回答
1

在这种情况下,请使用 jquery 的 GET、POST 或 AJAX 方法。
.loadjquery 的方法在内部只使用异步 http 请求,所以上面提到的所有方法。

于 2012-11-16T11:29:24.307 回答