简体   繁体   中英

Content of html div using jquery

I made an ajax call to a page and I receive some HTML code:

$.ajax({
    url: 'test.php',
    data: "id=1",
    cache: false,
    success: function(myHtml){
       //here I have myHtml 
    }
});

the returned html by test.php is myHtml and looks like:

<div id="firstDiv">
some text 123
</div>
<div id="firstDiv">
some text 456
</div>

How I get the content of firstDiv in jquery success?

Thank you.

You can use the jQuery constructor to build a jQuery object based on that code.

var results = $(myHtml);

In this case, you will have several elements in the selection, so you'll need to filter them, perhaps with eq in this case:

var firstResult = results.eq(0);

Note that there is no telling what jQuery will do with multiple instances of the same id in an HTML string.

$(myHTML).filter("div:first").text()

Try the following:

$("#firstDiv").get().innerHTML
$(myHtml).find("#firstDiv").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