来自iframe内容的jquery访问iframe id [英] jquery access iframe id from iframe content

查看:100
本文介绍了来自iframe内容的jquery访问iframe id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用jquery做点什么。

i am trying to do something with jquery.

我有这样的代码1.html;

i have code like this 1.html;

<html>
<head>
<script language="javascript" type="text/javascript" src="jquery.js"></script>
</head>
<body>
<iframe src="2.html" id="frame1"></iframe>
<iframe src="3.html" id="frame2"></iframe>
</body>
</html>

。我正在尝试访问iframe容器ID。 2.html文件内容;

in 2.html file. i am trying to access iframe container id. 2.html file content;

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function (){
    $('#btnIdKim').click(function (){
    }); 

});
</script>
</head>
<body>
<input type="button" name="btnIdKim" id="btnIdKim" value="Id Kim" />
</body>
</html>

当用户点击btnIdKim按钮时。我需要访问包含按钮的父iframe ID。

when user clicked btnIdKim Button. i need to access parent iframe id which contains by button.

但我不知道如何。

感谢所有感兴趣的人。

推荐答案

有一个HTML5(最初的IE扩展名)属性窗口上的ms533771%28v = VS.85%29.aspx> frameElement

There is an HTML5 (originally IE extension) property frameElement on window:

alert(frameElement.id);

但这不是全球支持的;你不会在谷歌浏览器(写作时;版本7)或旧版本的其他流行的非IE浏览器中获得它。

But this is not globally supported; you won't get it in Google Chrome (at the time of writing; version 7) or in older versions of the other popular non-IE browsers.

没有此扩展名,你必须手动完成。转到父窗口,搜索其所有iframe以查看其中包含的文档是否是您自己的文档:

Without this extension, you have to do it manually. Go to the parent window, and search through all its iframes to see if the document contained within is your own document:

function getFrameElement() {
    var iframes= parent.document.getElementsByTagName('iframe');
    for (var i= iframes.length; i-->0;) {
        var iframe= iframes[i];
        try {
            var idoc= 'contentDocument' in iframe? iframe.contentDocument : iframe.contentWindow.document;
        } catch (e) {
            continue;
        }
        if (idoc===document)
            return iframe;
    }
    return null;
}

alert(getFrameElement().id);

contentDocument 解决方法是必要的,因为IE< ; 8不支持它;需要 try ... catch 以避免安全异常在遇到来自不同域的帧时停止循环。)

(The contentDocument workaround is necessary because IE<8 doesn't support it; the try...catch is needed to avoid security exceptions stopping the loop when it meets a frame from a different domain.)

这篇关于来自iframe内容的jquery访问iframe id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆