简体   繁体   中英

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?.

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;"

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM