如何在jquery中获取匹配元素的html

发布于 2024-09-02 12:25:18 字数 255 浏览 0 评论 0原文

我有这个html,

<div id"test123">
dasd'asdasd

</div>

我想要那个div框的内容与 "

" 本身,而不仅仅是innerhtml

我尝试了 $('#test123') .html()

但它只给了我内部 html

I have this html

<div id"test123">
dasd'asdasd

</div>

I want the content of that div box with "<div id="test123">" itself as well not just innerhtml

I tried $('#test123').html()

But it only gave me inner html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

水中月 2024-09-09 12:25:18

另一种选择,对我来说看起来更线性/更好:
<代码>

var html = $('#foo').wrap('<div/>').parent().html();
$('#foo').unwrap();

found here:
http://codeasp.net/blogs/raghav_khunger/microsoft-net/751/jquery-how-to-get-html-of-a-div-with-that-div-tag-included

Another alternative, which looks more linear/nicer to me:

var html = $('#foo').wrap('<div/>').parent().html();
$('#foo').unwrap();


found here:
http://codeasp.net/blogs/raghav_khunger/microsoft-net/751/jquery-how-to-get-html-of-a-div-with-that-div-tag-included

牵强ㄟ 2024-09-09 12:25:18
var div = $('#test123').eq(0);
console.log(div);
var div = $('#test123').eq(0);
console.log(div);
如若梦似彩虹 2024-09-09 12:25:18

相当粗糙,但它完成了工作:

$('<div />').append($('#test123').clone()).html();

Pretty gnarly, but it gets the job done:

$('<div />').append($('#test123').clone()).html();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文