如何从内页获取父iframe元素而不知道id? [英] How to get parent iframe element from inner page without knowing id?
问题描述
< html>
...
< div>
< iframe src =test.htmlhash =r4d5f7>< / iframe>
< iframe src =test.htmlhash =8f7x97>< / iframe>
< iframe src =test.htmlhash =gg4v5e>< / iframe>
< iframe src =test.htmlhash =e54f87>< / iframe>
< / div>
...
< / html>
test.html是空页面,自定义哈希
属性始终具有不同的值,出于安全原因,两个页面都在同一个域上,iframe元素的数量和顺序是随机的。
我的问题是:是否有从内部页面(test.html)中使用Javascript到其正确的iframe元素的方法?让我们说,我在第三个iframe的页面,我需要得到它的iframe元素和alert()哈希值(在这种情况下是gg4v5e)。
更具体如果您熟悉Firefox的Firebug,可以像这样显示这种情况:
< html>
...
< div>
< iframe src =test.htmlhash =r4d5f7>
< html>
...
< / html>
< / iframe>
< iframe src =test.htmlhash =8f7x97>
< html>
...
< / html>
< / iframe>
< iframe src =test.htmlhash =gg4v5e>
< html>
...
< / html>
< / iframe>
< iframe src =test.htmlhash =e54f87>
< html>
...
< / html>
< / iframe>
< / div>
...
< / html>
可以调用something来获取父元素( < iframe>
)当我在内页的< html>
元素中使用我的Javascript时
当然有。
此代码适用于FF和IE6,Opera,Chrome(需要一个Web服务器,这两个文件来自同一域协议和端口)
code> function getHash(){
var ifs = window.top.document.getElementsByTagName(iframe);
for(var i = 0,len = ifs.length; i< len; i ++){
var f = ifs [i];
var fDoc = f.contentDocument || f.contentWindow.document;
if(fDoc === document){
alert(f.getAttribute(hash));
}
}
}
Let's imagine that I have something like this:
<html>
...
<div>
<iframe src="test.html" hash="r4d5f7"></iframe>
<iframe src="test.html" hash="8f7x97"></iframe>
<iframe src="test.html" hash="gg4v5e"></iframe>
<iframe src="test.html" hash="e54f87"></iframe>
</div>
...
</html>
test.html is empty page, custom hash
attribute always has a different value, both pages are on the same domain for security reasons, number and order of iframe elements is random.
My question is: Is there a way to get from inner page (test.html) using Javascript to its proper iframe element? Let's say, I'm in the third iframe's page and I need to get to its iframe element and alert() hash value (in this case "gg4v5e").
To be more specific. If you are familiar with Firefox's Firebug, it visualizes this situation like this:
<html>
...
<div>
<iframe src="test.html" hash="r4d5f7">
<html>
...
</html>
</iframe>
<iframe src="test.html" hash="8f7x97">
<html>
...
</html>
</iframe>
<iframe src="test.html" hash="gg4v5e">
<html>
...
</html>
</iframe>
<iframe src="test.html" hash="e54f87">
<html>
...
</html>
</iframe>
</div>
...
</html>
Is it possible to call "something" in order to get parent element (<iframe>
) when I'm with my Javascript at <html>
element in inner page?
Sure there is. This code works in FF and IE6, Opera, Chrome (requires a web server and both files coming from same domain protocol and port)
function getHash() {
var ifs = window.top.document.getElementsByTagName("iframe");
for(var i = 0, len = ifs.length; i < len; i++) {
var f = ifs[i];
var fDoc = f.contentDocument || f.contentWindow.document;
if(fDoc === document) {
alert(f.getAttribute("hash"));
}
}
}
这篇关于如何从内页获取父iframe元素而不知道id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!