Ajax 将.load的内容粘贴到变量中

Ajax 将.load的内容粘贴到变量中,ajax,jquery,jquery-load,Ajax,Jquery,Jquery Load,是否可以使用jQuery并将加载的内容粘贴到一个变量中,以便我以后可以使用该变量并将其附加到其他内容中?在这种情况下,请使用jQuery的GET、POST或AJAX方法。 jquery的.load方法在内部仅使用异步http请求,因此上述所有方法都可以,但这是不必要的。改为使用该方法直接访问响应 $.get("foo.php",function(response){ console.log(response); }); 如果您想同时执行这两项操作(即,将响应加载到div中并使用返回的数

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

在这种情况下,请使用jQuery的GET、POST或AJAX方法。

jquery的
.load
方法在内部仅使用异步http请求,因此上述所有方法都可以,但这是不必要的。改为使用该方法直接访问响应

$.get("foo.php",function(response){
    console.log(response);
});
如果您想同时执行这两项操作(即,将响应加载到div中并使用返回的数据),则可以使用
$方法上的回调。类似地,load
方法。

根据:

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

$('#result').load('ajax/test.html', function(responseText, textStatus, request) {
    alert(responseText);
});
其他方法是使用以下方法:

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

$.post('ajax/test.html', function(data) {
    $('.result').html(data);
    alert(data);
});
仅是以下内容的简写:

$.get(url, data, function(response) {
    $(selecton).replaceWith(response);
});

您可以在AJAX中尝试类似的方法

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

$.get(yourfile,function(response) {
     //div hidden with the html of your page
      $("#hiddendiv").html(response);
});
$.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);
});