简体   繁体   English

jQuery从var中删除元素

[英]Jquery remove element from var

I have a page with a parent div and several child divs. 我有一个包含父div和几个子div的页面。 These are generated dynamically. 这些是动态生成的。 I need to save the structure (html code) of all the child divs in my database, so I get the html content using .html(). 我需要在数据库中保存所有子div的结构(html代码),因此我使用.html()获取html内容。

But before I save the content in db, I need to remove one or more child divs. 但是在将内容保存到db中之前,我需要删除一个或多个子div。 (But these divs will still need to be on the browser page) (但是这些div仍然需要在浏览器页面上)

How do I use the remove method with the selector (in this example I need to remove child3) on the output of .html() 如何在.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();

how do I remove child3 from htmlContent? 如何从htmlContent删除child3?

Simply clone the element and operate on that: 只需克隆元素并对其进行操作:

var clone = $('#graph-parent').clone();
clone.find('#child3').remove();
var htmlContent = clone.html();

I think you need something like this: 我认为您需要这样的东西:

http://jsfiddle.net/glegan/65QPV/ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM