有多个iframe.
A是某个iframe中打开页面的元素,如何获取其所在iframe的ID?
jQueryiframe

解决方案 »

  1.   

    在A里面写个触发事件呗,比如说onclick什么的
    document.forms[0].id
      

  2.   


    function getIframeByElement(element){
        var iframe;
        $("iframe").each(function(){
            if(element.ownerDocument === this.contentWindow.document) {
                iframe = this;
            }
            return !iframe;
        });
        return iframe;
    }遍历所有的iframe,比对元素的ownerDocument和iframe的contentWindow的document,即可判断出元素属于哪个iframe。这段代码需要放在容纳iframe的那个页面中,而不是iframe内部。在iframe内部可以这样,例如:
    window.parent.getIframeByElement(document.body).id;//
    这就是获得自己的iframe的id。
      

  3.   

    TO:l676331991谢谢!但如果两个子页面完全一样,也能识别吗?另,为什么子页面中以下代码获取不到对应的父iframe的索引值,只能取到iframe最大的索引值呢?
    $("iframe",window.parent.document).index()
      

  4.   

    $("iframe",window.parent.document).each(function() {
        $(this).index();
    });