jQuery返回内容的包装器而不是内容

时间:2014-07-25 05:43:24

标签: jquery jquery-selectors element return-value

假设我有以下HTML

<articles>

    <article id="a1">
        <!-- Content of article with id="a1" -->
    </article>

    <article id="a2">
        <!-- Content of article with id="a2" -->
    </article>

    <article id="a3">
        <!-- Content of article with id="a3" -->
    </article>

</articles>

使用jQuery,我做了以下

var x = $("#a2").html();

现在,变量x将成为contian:

<!-- Content of article with id="a2" -->

但是,我希望x包含:

<article id="a2">
    <!-- Content of article with id="a2" -->
</article>

我试过了:

var x = $("#a2").parent().html();

但这返回了所有三篇文章,这不是我想要的。我只想要文章 a2 。我怎么能这样做?

感谢。

1 个答案:

答案 0 :(得分:1)

尝试使用.outerHTML属性

var x = $("#a2")[0].outerHTML;