开发者

How to get the id of iframe

document.getElementById(Iframe_id).contentWindow.addEventListener("blur", blurtest, true); 
开发者_如何学运维

By using this line I have given the blur event to iframe. it is working.

But When in

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

alert is used, but it gives value as Undefined.In other events it is working. So How to get the id of iframe in this blur function?.


you have 2 ways, to use scope closures to remember the variable during set up:

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

or using a proper way to get the source element of an event

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

good luck!


Try the automatic variable this. It should contain the element on which the event was fired.


When you load page into iFrmae, put into id of that iframe. So you can get the ID out of i by "this.getElementsByTagName('html')[0].id;"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜