页面上两个位置的相同 iframe

Same iframe in two places on the page?

本文关键字:位置 iframe 两个      更新时间:2023-09-26

我有一个奇怪的问题。我已经在页面上创建了一个 iframe,现在想把它放在两个不同的支架中:

 var iFrame = $('[data-id="loginWindowIframe"]');
    //put the iframe in first holder
    $('#loginWindow').html(iFrame);
    //put the iframe to the second holder
    $('[data-id="loginWindow"]').html(iFrame);

问题是,当我将其添加到第二个支架时,iframe 已从第一个支架中完全删除。似乎我只能在页面上有一个具有相同属性的 iframe 实例......

我做错了什么?

你只是在移动它。克隆它

var $iFrame = $('[data-id="loginWindowIframe"]');
//put the iframe in first holder
$('#loginWindow').empty().append($iFrame);
//put a clone of the iframe to the second holder
$('[data-id="loginWindow"]').empty().append($iFrame.clone());

请注意,我使用了$iFrame而不是iFrame。这纯粹是为了可读性 - 它表示它包含一个jQuery对象。