JQuery AJAX HTML查找不起作用 [英] JQuery AJAX HTML Find Not Working

查看:101
本文介绍了JQuery AJAX HTML查找不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO上读过类似的Q / A,但这个简单的JQuery AJAX代码仍然让我疯狂 -

I've read similar Q/A on SO but this simple piece of JQuery AJAX code is still driving me nuts --

$.get($('#pager a').attr('href'), function(data) {
    var data = $(data).wrap("<div />");
    console.log(data);
    console.log(data.find('.content'));
    console.log(data.find('#next_page'));
}, "html");

AJAX调用返回的HTML是 -

The HTML returned by the AJAX call is --

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head></head>
  <body>
    <div class="content">lorum</div>
    <div class="content">ipsum</div>
    <div id="next_page">
      <a href="/page/2"></a>
    </div>
  </body>
</html>

控制台日志 -

[<TextNode textContent="\n\n \n \n ">, div.content, <TextNode textContent="\n ">, div.content, <TextNode textContent="\n ">, div#next_page, <TextNode textContent="\n \n">]
[ ]
[ ]

我不能为我的生活弄清楚为什么data.find('。content')和data.find ('#next_page')什么都不匹配。

I can't for my life figure out why data.find('.content') and data.find('#next_page') match nothing.

任何指针都会非常感激!

Any pointer will be greatly appreciated!

推荐答案

来自 .wrap() [docs] 文档:


此方法返回原始集用于链接目的的元素。

This method returns the original set of elements for chaining purposes.

在这种情况下,元素集是 body <的所有子节点/ code>。 .find()将搜索这些节点的后代,但不搜索节点本身。

The set of elements in this case are all the child nodes of body. .find() will search for descendants of these nodes, but not the nodes itself.

使用 .filter() [docs] 或者创建一个空的 div 并设置 data 作为其内容: / p>

Either use .filter() [docs] instead or create an empty div and set data as its content:

var data = $('<div />').html(data);

这篇关于JQuery AJAX HTML查找不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆