首页 > 解决方案 > 如果内容中存在 iframe,如何将 iframe / 视频替换为另一个 div?

问题描述

WordPress 网站。

我想要 jquery 或 php,或者任何可以从 div id=content(如果存在)中获取 div 的简单解决方案,并将其放在 id=iframer div 中,同时在加载时将其删除到 id=content 中。

<div id="iframer">
**/ I am waitting to get "<div class="video">All content </div>" inside me. /**
</div>

<div id="content">
<div class="video">
<iframe width="697" height="392" src="https://www.youtube.com/embed/xxxxxxx?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>
</div>
</div>

一些脚本后的结果......

<div id="iframer">
<div class="video">
<iframe width="697" height="392" src="https://www.youtube.com/embed/xxxxxxx?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>
</div>
</div>

<div id="content">
**/ Iframer stolen my content. /**
</div>

标签: phphtml

解决方案


不知道为什么您不能直接编辑 HTML 或生成此 HTML 的代码,但这是我使用 javascript/jQuery 的解决方案。

<script>
//A page can't be manipulated safely until the document is "ready."
$( document ).ready(function() {
  //Get the html content inside div with id='content'
  var myHtml = $( '#content' ).html();

  //Copy the HTML in the div with id='iframer'
  $( '#iframer' ).html( myHtml );

  //Delete the HTML content inside div with id='content'
  $( '#content' ).html( '' );
});
</script>

推荐阅读