简体   繁体   中英

Reading a portion of the ajax html response

My ajax (jquery) response (html) gives me a whole junk of html source code since it fetches the enrite page.

The response is somewhat like below:


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

.......

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

</body>
</html>

I need help with the following:

1) Is it possible to read just whatever is between the <div id='content'></div> ? if yes, how?

2) if #1 is not possible, how could I extract just the content from the <div id='content'></div> ?

The code I tried:


    /*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();
        });
    });

Any help on this is much appreciated

Many thanks, Racky

那这个呢?

$("#content", results).html()

Yes, it is possible, but you need to make a jQuery object out of it first.

Change this line:

alert (results.find("div[id='content']").html());

to this:

alert ( $(results).find("div[id='content']").html() );

or, better yet, to this:

alert ( $(results).find("#content").html() );

EDIT:

Looks like if the element you want is a direct child of body , you'll need to use .filter() instead since the body seems to be excluded from the jQuery object. Looks like just its contents are included.

alert ( $(results).filter("#content").html() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM