简体   繁体   中英

How do I get the ID of iframe's parent div?

I have a page that looks like this:

 <div class="myclass" id="div1"><iframe id="frame1" src="myiframe.html"></div>
 <div class="myclass" id="div2"><iframe id="frame2" src="myiframe.html"></div>

In my iframe , I need to know the id of the div of its parent node, but I don't know how. I searched for a solution without jquery but all I found was something with getElementById stuff and I am actually searching for the id.

Can someone help me?

You must know that, as far as your JS code inside the iFrame is concerned, the window is the iFrame, and the document is the content of that iFrame. Here's a little function that should work for you, in order to the parent's document:

var parentDoc = window;
while(parentDoc !== parentDoc.parent)
{
    parentDoc = parentDoc.parent;
}
parentDoc = parentDoc.document;
var iFrames = parentDoc.getElementsByTagName('iframe');
var divs = [];
for(var i=0;i<iFrames.length;i++)
{
    divs.push(iFrames[i].parentNode);//assuming the first parent is the div
}

That should do the trick

try like this:

parent.document.getElementById("div id")

var parentDivOfIfame1 = $("#frame1", parent.document).parent();

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