如何使用 JQuery 在 AJAX 回调中查找元素?
每当我尝试查找 AJAX 返回页面的正文时,它都会返回 null。这是我的 JQuery 代码:
$(document).ready(function(){
$("#button").click(function(){
$.ajax({
url: 'testpagec.php',
type: 'GET',
success: function(data) {
var test = $(data).find('body');
alert(test.html());
}
});
return false;
});
});
这在纸面上看起来很完美,但它没有按预期工作。有解决这个问题的想法吗?谢谢。
Whenever I try to find say, the body of the returned page from AJAX, it returns null. Here's my JQuery code:
$(document).ready(function(){
$("#button").click(function(){
$.ajax({
url: 'testpagec.php',
type: 'GET',
success: function(data) {
var test = $(data).find('body');
alert(test.html());
}
});
return false;
});
});
This looks perfect on paper but it's not working as intended. Any ideas on fixing this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我有用的是
上面的原因是因为
body
不是祖先,因此find
不起作用,为数据提供根级元素将确保 find 有效。您还可以使用.filter
。What works for me is this
The reason the above works is because
body
was not an ancestor hencefind
didn't work, giving the data a root level element will ensure find works. You can also use.filter
.