简体   繁体   English

使用jQuery将所有HTML内容从一个div复制到另一个div

[英]Copying all HTML contents from one div to another using jQuery

This question may seem as a duplicate one, but I cannot seem to find a solution for my problem. 这个问题可能看似重复,但我似乎无法找到解决问题的方法。

I am fetching content from external websites using the iFramely API. 我使用iFramely API从外部网站获取内容。 Which works for almost all requested websites. 适用于几乎所有要求的网站。

However, if I try to fetch for example a post from Reddit and display this in a sort of custom made 'preview box' the requested iFrame get rendered correctly, so far so good. 但是,如果我尝试从Reddit中获取一个帖子并将其显示在一个自定义的“预览框”中,则所请求的iFrame会正确呈现,到目前为止一切都很好。

When I try to copy the HTML content of the 'preview box' in order to append it to another div using jQuery. 当我尝试复制'预览框'的HTML内容,以便使用jQuery将其附加到另一个div。 It does not copy all HTML elements. 它不会复制所有HTML元素。

Complete function 功能齐全

$('#btn-add-extern-snippet').click(function() {
    let embed_preview_container = $('#embed-preview');
    let embed_target_number = $('#embed-target-number');
    let target_number = $(embed_target_number).val();

    $('.embed-no-' + target_number).html($(embed_preview_container).html());
    ...
});

I've tried the jQuery functions: 我试过jQuery函数:

  • clone $('.embed-no-' + target_number).html($(embed_preview_container).clone()); clone $('.embed-no-' + target_number).html($(embed_preview_container).clone());
  • html $('.embed-no-' + target_number).html($(embed_preview_container).html()); html $('.embed-no-' + target_number).html($(embed_preview_container).html());
  • contents $('.embed-no-' + target_number).html($(embed_preview_container).contents()); contents $('.embed-no-' + target_number).html($(embed_preview_container).contents());
  • using append $('.embed-no-' + target_number).append($(embed_preview_container).html()); using append $('.embed-no-' + target_number).append($(embed_preview_container).html());

But all of these functions do not seem the copy the full "innerHTML" of the mentioned 'preview box' (see pictures below). 但是所有这些功能似乎并不是所提到的“预览框”的完整“innerHTML”的副本(见下图)。

The copied HTML content inside the 'other div' with an empty body & head. 复制的HTML内容在“其他div”中,空体和头部为空。 复制的HTML内容具有空主体和头部

The actual content in side the 'preview box'. “预览框”旁边的实际内容。 “预览框”旁边的实际内容

I must note here that this only seems to be te case for Reddit related content. 我必须在此注意,这似乎只适用于Reddit相关内容。 It works for example with Soundcloud/Spotify/Bandcamp . 它适用于Soundcloud / Spotify / Bandcamp

PS. PS。 I am using jQuery version 3.3.1 我正在使用jQuery版本3.3.1

Code sample below:- 代码示例如下: -

$("#copyToDiv").append($("#copyFromDiv").html());

Thanks 谢谢

you can try this. 你可以试试这个。

  $('#copy').click(function(){ $('#div2').html($('#div1').html()); }); 
 #div1{ background:green; } #div2{ background:blue; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id='copy'>Copy</button> <div id='div1'> <p>Le Lorem Ipsum est simplement du faux texte employé dans la composition et la mise en page avant impression. Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen de polices de texte. Il n'a pas fait que survivre cinq siècles, mais s'est aussi adapté à la bureautique informatique, sans que son contenu n'en soit modifié. Il a été popularisé dans les années 1960 grâce à la vente de feuilles Letraset contenant des passages du Lorem Ipsum, et, plus récemment, par son inclusion dans des applications de mise en page de texte, comme Aldus PageMaker. </p> <div> <h2>Lorem Ipsum</h2> <p>Le Lorem Ipsum est simplement du faux texte employé dans la composition et la mise en page avant impression. Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen de polices de texte. Il n'a pas fait que survivre cinq siècles, mais s'est aussi adapté à la bureautique informatique, sans que son contenu n'en soit modifié. Il a été popularisé dans les années 1960 grâce à la vente de feuilles </p> </div> <div id='div2'></div> 

Alright, so I've found the solution/workaround to my own problem. 好吧,所以我找到了解决方案/解决方法来解决我自己的问题。 Instead of copying the contents of the 'preview box', I stored the fetched HTML in a temporary variable, and appended the contents of this variable inside an 'another div' (this works). 我没有复制“预览框”的内容,而是将获取的HTML存储在临时变量中,并将此变量的内容附加到“另一个div”中(这样可行)。

Noteworthy, something strange that occurred to me while debugging is that the browser (in this case Chrome), does not render/display HTML correctly (see picture) (note the empty iFrame). 值得注意的是,在调试时我发现的奇怪之处在于浏览器(在本例中为Chrome),无法正确呈现/显示HTML(参见图片)(请注意空iFrame)。

在此输入图像描述

This is exactly what .html() copied. 这正是.html()复制的内容。 So long story short the following code worked for me: 长话短说以下代码对我有用:

// temporary variable for storing html.
let currentSnippet = '';

$('#btn-add-extern-snippet').click(function() {
    let embed_preview_container = $('#embed-preview');
    let embed_target_number = $('#embed-target-number');
    let target_number = $(embed_target_number).val();

    $('.embed-no-' + target_number).html(currentSnippet);
    ...
});

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

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