jQuery:获取包含选择器的HTML? [英] jQuery: Get HTML including the selector?

查看:90
本文介绍了jQuery:获取包含选择器的HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个HTML:

 < ul> 
< li id =example>< strong> Awesome< / strong>示例文本< / li>
< / ul>

我希望能够像 $('#example' ).html()但现在做这件事显然只会得到< strong> Awesome< / strong>示例文本



那么,如何才能获取包含所选元素的HTML



即。 < li id =example>< strong> Awesome< / strong>示例文本< / li>



我正在使用jQuery 1.4.4。



  var outerHTML = $(< div />。;)附加($( '#例子')克隆())的HTML()。; 

请参阅本页了解更多详情。



以及此处的讨论:< a href =http://api.jquery.com/html/ =nofollow noreferrer> http://api.jquery.com/html/ (这解释了这不是在jQuery中核心以及为什么一些逻辑解决方案无法正常工作)

Say I have this HTML:

<ul>
  <li id="example"><strong>Awesome</strong> example text</li>
</ul>

I want to be able to do something like $('#example').html() but right now doing that obviously only gets <strong>Awesome</strong> example text.

So how can I get the HTML including the selected element?

ie. <li id="example"><strong>Awesome</strong> example text</li>

I'm using jQuery 1.4.4.

解决方案

In this specific case:

var outerHTML = $("<div />").append($('#example').clone()).html();

See this page for more details.

And the discussion here: http://api.jquery.com/html/ (this explains that this isn't in jQuery core and why some logical solutions won't work)

这篇关于jQuery:获取包含选择器的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆