Javascript:在加载的页面中获取 iframe id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5864505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: get iframe id within loaded page
提问by Pav
I've got a page that has an iframe. Every time the page loads, iframe gets unique id and name assigned to it. I can get the iframe name within loaded iframe like so:
我有一个带有 iframe 的页面。每次页面加载时,iframe 都会获得分配给它的唯一 ID 和名称。我可以在加载的 iframe 中获取 iframe 名称,如下所示:
alert(parent.window.frames[window.name].name);
But when i try to get the id value:
但是当我尝试获取 id 值时:
alert(parent.window.frames[window.name].id);
I get undefined
?
我明白了undefined
吗?
Is it possible to get the id attribute of the iframe within loaded page?
是否可以在加载的页面中获取 iframe 的 id 属性?
<iframe id="lyygi8stwZSANUEh" src="http://example.com" name="zma82vRVe18xbAqW" title="Awesome Iframe">
example.com:
例子.com:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Awesome Iframe</title>
</head>
<body>
<script type="text/javascript">
alert(parent.window.frames[window.name].name);
</script>
</body>
</html>
回答by Boris Zbarsky
Try window.frameElement.id
in the iframe.
window.frameElement.id
在 iframe 中尝试。
回答by Michael Brüggemann
In the iframe this is working for me. But only if the iframe and the parent share the same domain:
在 iframe 中,这对我有用。但前提是 iframe 和父级共享同一个域:
this.frameElement.attributes.id
回答by user8788753
You have to insert the name of the iframe:
您必须插入 iframe 的名称:
alert(parent.window.frames['zma82vRVe18xbAqW'].name);