我有第三方 (mootools) 库创建标签,我有 google double click for publishers (dfp) 创建广告。 dfp 在 iframe 中创建广告,然后标签脚本获取 iframe 的 anchor 并“混淆”它以创建标签。 iframe 的内容会在此过程中丢失。
我正在寻找解决此问题的方法(尝试在标签加载后触发 dfp 内容,但随后 google 脚本崩溃了)。
iframe 来自与父窗口不同的域,因此任何试图对 iframe 中的元素进行操作的操作都将失败。
addTab: function(text, title, content) {
var grab = $(content);
var container = (grab || new Element('div'))
.setStyle('display', 'none')
.addClass(this.options.classContainer);
this.wrapper.adopt(container);
var pos = this.tabs.length;
var evt = (this.options.hover) ? 'mouseenter' : 'click';
var tab = {
container: container,
toggle: new Element('li').grab(new Element('a', {
href: '#',
title: title
}).grab(
new Element('span', {html: text})
)).addEvent(evt, this.onClick.bindWithEvent(this, [pos])).inject(this.menu)
};
if (!grab && $type(content) == 'string') tab.url = content;
this.tabs.push(tab);
return this.fireEvent('onAdded', [tab.toggle, tab.container, pos]);
},
最佳答案
每当 iframe 或其祖先与 DOM 断开连接时,iframe 的内容将被清除,iframe 将在它或其祖先重新插入时重新加载。这种烦人的行为发生在 Firefox 和 Webkit 中,但我认为 iframe 不会在 IE 上重新加载。
作为解决方法,您需要找到一种方法来重新排序您的代码,以便仅在选项卡脚本完成对容器的操作后添加 iframe。
如果您使用 adoptNode 而不是 apendChild 移动 iframe,Chrome 显然还有一个选项会导致 iframe 不重新加载,但我不认为它是跨浏览器的。
有关更多信息,请参阅此类似问题:How to move an iFrame in the DOM without losing its state?
关于javascript - iframe 内容在被 mootools 操作时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8445390/