哞.无法在 iframe 中获取 DOM 元素

mootools. Can't get DOM elements within iframe

本文关键字:获取 DOM 元素 iframe      更新时间:2023-09-26

我有一个基于SocialEngine 4的网站,它使用moo-tools-1.2.5。有一个带有一些 js 的 iframe,它会在弹出窗口中打开(带有内置的 Smoothbox 插件)。而且我无法仅通过调用 $$() 从 JS 获取这个 iframe 的 DOM 元素,我需要使用这样的 kludge:

var context = (window.frames[0]) ? window.frames[0] : window;
context.$('user_email').set('value', context.$('1_1_30').get('value'));

这很奇怪,我想这是意外的行为,因为标准 SocialEngine 模块中的 JS 在 iframe 中也不起作用。

这是因为在 1.2.5 中,$$this.document.getElements 的别名。

我建议你使用mootools-core的IFrame包装器类,它可以扩展iframe,这样它们就会变得mootoolsy,然后你可以做这样的事情:

var context = this.document, // window
    iframe = document.getElement("iframe")
// if iframe found, change context to its contentDocument
iframe && (context = new IFrame("foo").contentDocument)
// wrapper.
!function() {
    console.log(this.getElements("div"))
}.call(context)