2012-04-13 31 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