Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回特定HTML元素的jQuery ajax调用_Jquery_Html - Fatal编程技术网

返回特定HTML元素的jQuery ajax调用

返回特定HTML元素的jQuery ajax调用,jquery,html,Jquery,Html,我尝试了以下代码: $.post('/script', function(result) { var foo = $(result).find('#foo'); $('#result').html(foo); }); 以下是返回的html: <div id='foo'> Content. </div> <div id='new'> New data </div> 这(var内容)也会导致object并在#result上

我尝试了以下代码:

$.post('/script', function(result) {
    var foo = $(result).find('#foo');
    $('#result').html(foo);
});
以下是返回的html:

<div id='foo'>
  Content.
</div>
<div id='new'>
   New data
</div>

这(var内容)也会导致object并在#result上打印一个空字符串。

这是因为
#foo
#new
都处于顶层

.find()
方法查找子体。因此,您需要在服务器端(result)或jQuery级别将它们包装到另一个div中

因此,要么将html更改为

<div>
   <div id='foo'>
     Content.
   </div>
   <div id='new'>
      New data
   </div>
</div>

这将从
/script
页面加载id为
foo
的元素,并将其放入
#result
元素中。

这是因为
#foo
#new
都处于顶级

.find()
方法查找子体。因此,您需要在服务器端(result)或jQuery级别将它们包装到另一个div中

因此,要么将html更改为

<div>
   <div id='foo'>
     Content.
   </div>
   <div id='new'>
      New data
   </div>
</div>

这将从
/script
页面加载id为
foo
的元素,并将其放入
#result
元素。

foo是一个对象。。。html()是您想要的,因此您必须更改: $('#result').html(foo); 在里面
$('#result').html(foo.html())

foo是一个对象。。。html()是您想要的,因此您必须更改: $('#result').html(foo); 在里面
$('#result').html(foo.html())

但我想检索#foo的内容,只留下#newdiv@markcruz,这就是这段代码所做的(所有版本都只选择#foo元素。只是您需要准备一点数据。)但我想检索#foo的内容,只保留#新的div@markcruz,这就是该代码的作用(所有版本都只选择#foo元素。只是您需要准备一些数据。)
$.post('/script', function(result) {
    var foo = $(result).wrapAll('<div>').parent().find('#foo');
    $('#result').html(foo);
});
$('#result').load('/script #foo');