2012-04-13 28 views
2

我有一个基于SocialEngine 4的网站,该网站使用moo-tools-1.2.5。 有一个iframe,其中有一些js会在弹出窗口中打开(带有内置的Smoothbox插件)。 我不能只是通过调用得到这个IFRAME从JS纳入它的DOM元素$$(),我需要使用像这样的杂牌组装电脑:摩托车。无法在iframe中获取DOM元素

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

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

回答

1

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

我建议你使用IFrame wrapper class从MooTools的核心,可扩展的I帧,使他们得到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) 
+0

感谢的答案,但我意味着JS代码位于在iframe中。我不明白为什么它在父母的背景下运行,而不是它自己的。 – 2012-04-13 11:40:09