简体   繁体   中英

How can I get a specific div from an outside iframe when I am already within a different iframe

I have issues related to the following code

This is my main Page:

<html>
    <body>
    <div class="pagewrapper"> 
        <div class="page">
                <iframe id="TabContentIframe" 
                    src="/MyTestWebsite4/TabContent/Details/8">
                </iframe>
                <iframe id="TabContentIframe" 
                    src="/MyTestWebsite4/TabContent/Details/8">
                </iframe>
                <iframe id="TabContentIframe" 
                    src="/MyTestWebsite4/TabContent/Details/8">
                </iframe>
            </div>
        </div> 
    </body>
</html>

The TabContentIframe looks Like This:

<html>
    <body>
        <div class="pagewrapper"> 
            <div class="DivIWant">
                <iframe id="MainContentIframe" 
                    src="/MyTestWebsite4/MainContent/Details/8">
                </iframe>
            </div>
        </div> 
    </body>
</html>

And The MainContentIframe Looks Like This:

<html>
    <body>
        <div class="pagewrapper"> 
            <div iD = "BtnIPressed"></div> 
        </div> 
    </body>
</html>

As you can see there are all in the same domain.

If I press the BtnIPressed , how can I get the DivIWant that is closest to the iframe within I pressed the button?

Orginal Answer was:

alert(parent.parent.getElementById("DivIWant").innerHTML)

But now you changed the location of the div so it would just be one level up.

alert(parent.getElementById("DivIWant").innerHTML)

Walking up parents until you find a matching div

var par = null;
var div = null;
while(par!==window){
    par = par.parent;
    var div = par.getElementById("DivIWant")
    if(div){       
         break;
    }
}
if(div){
    alert(div.innerHTML);
}

Try this

window.top.document.getElementById("DivIWant");

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