阅读ajax html响应的一部分

rac*_*cky 5 html javascript jquery

我的ajax(jquery)响应(html)给了我一大堆html源代码,因为它获取了enrite页面.

响应有点像下面:


<html>
<head>
...
...
</head>
<body>
.
.....
.

.......

<div id="content">
    content i want to extract
</div>
.............
..........
.............

</body>
</html>

我需要以下方面的帮助:

1)是否可以阅读之间的任何内容<div id='content'></div>?如果有,怎么样?

2)如果#1不可能,我怎样才能从中提取内容<div id='content'></div>

我试过的代码:


    /*Ajax*/
    $jq(function(){
    alert ("Doc ready");
        $jq("#content a.next-page").bind("click", function(e){
            alert ("Hey!");
            /*Make the call*/
            $jq.ajax({
                url: "/page/2",
                type: "get",
                cache: false,
                data: "",
                error: function(){alert ("No data found for your search.");},
                success: function(results){
                    //$searchPanel.find("tbody").empty().append(results);
                    //$searchPanelHolder.css({"display":"block"});
                    alert (results.find("div[id='content']").html());
                    e.preventDefault();
                }
            });
            e.preventDefault();
        });
    });

对此有任何帮助非常感谢

非常感谢,Racky

use*_*716 0

是的,这是可能的,但您需要先用它创建一个 jQuery 对象。

更改这一行:

alert (results.find("div[id='content']").html());
Run Code Online (Sandbox Code Playgroud)

对此:

alert ( $(results).find("div[id='content']").html() );
Run Code Online (Sandbox Code Playgroud)

或者,更好的是:

alert ( $(results).find("#content").html() );
Run Code Online (Sandbox Code Playgroud)

编辑:

看起来如果您想要的元素是 的直接子元素body,则需要使用.filter(),因为body似乎被排除在 jQuery 对象之外。看起来只包含其内容。

alert ( $(results).filter("#content").html() );
Run Code Online (Sandbox Code Playgroud)