2009-12-18 111 views
0
document.getElementById(Iframe_id).contentWindow.addEventListener("blur", blurtest, true); 

通過使用此行,我已將blur事件提供給iframe。這是工作。如何獲取iframe的ID

但是當

function blurtest(e) 
{ 
    alert(e.target.id); 
} 

警報被使用,但它提供了價值,因爲它是工作Undefined.In其他事件。那麼如何在這個模糊函數中獲得iframe的id?

+0

你需要爲iframe添加一個名稱屬性,你做了那個 – 2009-12-18 10:08:57

回答

0

你有2種方式,使用範圍封鎖記住在變量設置:

var theID = Iframe_id; 
document.getElementById(Iframe_id).contentWindow.addEventListener("blur", function(){ blurtest(theID), true); 

或使用適當的方式來獲得一個事件

function blurtest(e) 
{ 
    e = e || window.event; 
    var obj = e.srcElement ? e.srcElement : e.target; 
    alert(obj.getAttribute('id')); 
} 

好運的源元素!

+0

非常感謝。這真是一個很好的解決方案。 – Pranjali 2009-12-18 11:55:20

+0

然後給我點擊一下,:D – 2009-12-18 18:52:52

0

嘗試自動變量this。它應該包含事件被觸發的元素。

0

當您將頁面加載到iFrmae中時,將其放入該iframe的ID中。所以你可以通過「this.getElementsByTagName('html')[0] .id;」