我有一个iframe,为了访问父元素,我实现了以下代码:

window.parent.document.getElementById('parentPrice').innerHTML

如何使用jquery得到相同的结果? 更新:或者如何使用jquery访问iFrame父页?


当前回答

你可以使用window从iframe中访问父窗口的元素。父母是这样的:

// using jquery    
window.parent.$("#element_id");

也就是:

// pure javascript
window.parent.document.getElementById("element_id");

如果你有多个嵌套iframe并且你想要访问最上面的iframe,那么你可以使用window。上面像这样:

// using jquery
window.top.$("#element_id");

也就是:

// pure javascript
window.top.document.getElementById("element_id");

其他回答

是的,对我来说也很管用。

注意:我们需要使用window.parent.document

    $("button", window.parent.document).click(function()
    {
        alert("Functionality defined by def");
    });

要在iFrame的父类中查找,使用:

$('#parentPrice', window.parent.document).html();

$()包装器的第二个参数是要搜索的上下文。默认为document。

你可以使用window从iframe中访问父窗口的元素。父母是这样的:

// using jquery    
window.parent.$("#element_id");

也就是:

// pure javascript
window.parent.document.getElementById("element_id");

如果你有多个嵌套iframe并且你想要访问最上面的iframe,那么你可以使用window。上面像这样:

// using jquery
window.top.$("#element_id");

也就是:

// pure javascript
window.top.document.getElementById("element_id");

可能有点晚了,但我刚刚发现了这个神奇的jQuery插件https://github.com/mkdynamic/jquery-popupwindow。它基本上使用一个onUnload回调事件,所以它基本上监听子窗口的关闭,并将在那一点上执行任何必要的东西。所以真的没有必要在子窗口中写任何JS来传递回父窗口。

这对我来说很管用。 在我的情况下,我必须从POPUP JS填充值到父窗口形式。

所以我已经使用$('#ee_id',window.open .document).val(eeID);

优秀的! !