返回特定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');