繁体   English   中英

如何将div附加到iframe的父项?

[英]How to append a div to an iframe's parent?

我想在iframe(主窗口)的父窗口中创建一个div。 我从没想过我会为此而努力。

我尝试了以下方法:

parent.document.createElement('div')
window.parent.document.createElement('div')
window.parent.document.getElementsByTagName('body')[0].createElement('div')

iframe的属性如下:

scrolling: 'no',
width: 400,
height: 125,
frameborder: 0,
sandbox: 'allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox'

我想澄清一下该脚本是在iframe中调用的,因为据我所知,以前可能还不清楚。

您需要执行以下步骤来根据需要修改dom树:

  1. 获取iframe的父项
  2. 创建div元素
  3. 在iframe之前插入新创建的div

这是代码(对我来说很好):

 var iframe = document.getElementsByTagName('iframe')[0];
 var iframeParent = iframe.parentElement;
 var div = document.createElement('div');
 iframeParent.insertBefore(div, iframe);

暂无
暂无

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

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