简体   繁体   English

html div 的内容使用 jquery

[英]Content of html div using jquery

I made an ajax call to a page and I receive some HTML code:我对页面进行了 ajax 调用,我收到了一些 HTML 代码:

$.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:通过 test.php 返回的 html 是 myHtml,如下所示:

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

How I get the content of firstDiv in jquery success?如何获得 jquery 中 firstDiv 的内容成功?

Thank you.谢谢你。

You can use the jQuery constructor to build a jQuery object based on that code.您可以使用jQuery 构造函数基于该代码构建 jQuery object。

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:在这种情况下,您将在选择中包含多个元素,因此您需要过滤它们,在这种情况下可能使用eq

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.请注意,不知道 jQuery 会对 HTML 字符串中相同id的多个实例做什么。

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

Try the following:尝试以下操作:

$("#firstDiv").get().innerHTML
$(myHtml).find("#firstDiv").html()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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