jQuery获取包含容器本身的容器的html [英] jQuery get html of container including the container itself

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

问题描述

我如何获得'#container'上的html,包括'#container',而不仅仅是内部的内容。

How do i get the html on '#container' including '#container' and not just what's inside it.

<div id="container">
 <div id="one">test 1 </div>
 <div id="two">test 2 </div>
 <div id="three">test 3 </div>
 <div id="four">test 4 </div>
</div>

我有这个获取#container里面的html。它不包括#container元素本身。这就是我要做的事情

I have this which gets the html inside #container. it does not include the #container element itself. That's what i'm looking to do

var x = $('#container').html();
$('#save').val(x);

检查 http://jsfiddle.net/rzfPP/58/

推荐答案

如果你包装容器在虚拟 P 标记中,您也将获得容器HTML。

If you wrap the container in a dummy P tag you will get the container HTML also.

您需要做的只是

var x = $('#container').wrap('<p/>').parent().html();

检查 http://jsfiddle.net/rzfPP/68/

Check working example at http://jsfiddle.net/rzfPP/68/

unwrap() < p> 标签完成后,你可以添加

To unwrap()the <p> tag when done, you can add

$('#container').unwrap();

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

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