简体   繁体   English

获取最外层div元素的HTML(最高父级)

[英]Getting the HTML of outermost div element (Highest Parent)


I have a div that I fetch dynamically and it is structured as below. 我有一个动态获取的div ,其结构如下。

      <div id="mainWrap">
            <div style="z-index: 1001; height: 407px; width: 328px; top: 150px; left: 601.5px;" id="com-TMRJDOR2KOET3X6JPV6XGU0FB7RGJ926" class="textBox contentBox">
               <div style="" class="editable"><p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, </p>
               </div>
            </div>

            <div style="z-index: 1002; height: 616px; width: 288px; top: 29px; left: 3.5px;" id="com-UPWTMKZ7OUTN8KG2JEK47LNPN5JO261H" class="textBox contentBox">
               <div style="" class="editable"><p>
            ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
              </div>
           </div>            
      </div>

Now,I want to remove the ids of all the children which I achieved like this. 现在,我想删除我像这样实现的所有孩子的ID。

      var getContent = $('#mainWrap');
      var finalContent = getContent.clone().children().removeAttr('id');
      var saveContent  = finalContent.parent().html();

But I get the inner div's with out the outermost div ie mainWrap .I also want the mainWrap div to be returned in the html I get. 但是我得到了内部div而没有最外面的div即mainWrap 。我也希望将mainWrap div返回到我得到的html中。

This is what I tried to do,worked to some extent: 这是我尝试做的,在某种程度上有效:

    $.fn.getOuter = function(val){
      return $('<div class="temp">').append($('#'+val).clone()).html();
    }
    var htmlPage = $.fn.getOuter('mainWrap');

If I do alert(htmlPage) I get the entire content.But when I try to remove the id attributes using this var content=$(htmlPage).children().removeAttr('id'); 如果我发出alert(htmlPage)我会得到全部内容。但是当我尝试使用此var content=$(htmlPage).children().removeAttr('id');删除id属性时var content=$(htmlPage).children().removeAttr('id'); I don't get the correct HTML.I still get only the children inside mainWrap .I am not sure where am I wrong.Any help would be appreciated. 我没有正确的HTML.mainWrap中仍然只有孩子。我不确定我在哪里mainWrap 。任何帮助将不胜感激。
Thanks for your time. 谢谢你的时间。

The problem is because when you do .parent() in final content, there is no parent as you've cloned the element into memory. 问题是因为当您在最终内容中执行.parent()时,因为已将元素克隆到内存中,所以没有父级。 Instead, wrap it in another div and get the html() of that. 相反,将其包装在另一个div并获取其html() I have also amended the logic slightly, try this: 我也略微修改了逻辑,请尝试以下操作:

var $getContent = $('#mainWrap').clone();
$getContent.children().removeAttr('id');
var saveContent  = $getContent.wrap('<div></div>').parent().html();

Here is a simple example that works ( JSFiddle ): 这是一个简单的示例( JSFiddle ):

<div id="id1">
    <span>some text</span>
    <div id="someid">some more text</div>
</div>

<textarea id="ta"></textarea>

var getContent = $('<div>').append($("#id1").clone());
getContent.find("*").removeAttr("id");
$("#ta").val( getContent.html() );

You can append the cloned elements into a div on the fly. 您可以随时将克隆的元素附加到div I use find("*") because children() only gets the direct descendants of getContent . 我使用find("*")因为children()仅获取getContent的直接后代。 I assumed you wanted to remove all id attributes regardless of how deep they were. 我假设您想删除所有id属性,无论它们有多深。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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