[英]Jquery remove element from var
我有一个包含父div和几个子div的页面。 这些是动态生成的。 我需要在数据库中保存所有子div的结构(html代码),因此我使用.html()获取html内容。
但是在将内容保存到db中之前,我需要删除一个或多个子div。 (但是这些div仍然需要在浏览器页面上)
如何在.html()的输出中将remove方法与选择器一起使用(在此示例中,我需要删除child3)
<div id="graph-parent">
<div id="child1"></div>
<div id="child2"></div>
<div id="child3"></div>
<div id="child4"></div>
<div id="child5"></div>
</div>
var htmlContent = $( "#graph-parent" ).html();
如何从htmlContent删除child3?
只需克隆元素并对其进行操作:
var clone = $('#graph-parent').clone();
clone.find('#child3').remove();
var htmlContent = clone.html();
我认为您需要这样的东西:
http://jsfiddle.net/glegan/65QPV/
var clone = $('#graph-parent').clone();
clone.find('#child1').remove();
clone.find('#child5').remove();
var htmlContent = clone.html();
alert(htmlContent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.