开发者

How to get the id of iframe

开发者 https://www.devze.com 2022-12-14 13:53 出处:网络
document.getElementById(Iframe_id).contentWindow.addEventListener(\"blur\", blurtest, true); 开发者_如何学运维By using this line I have given the blur event to iframe. it is working.
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

精彩评论

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

关注公众号