简体   繁体   English

如何从父窗口访问iframe中存在的元素?

[英]How to access element present inside a iframe from parent window?

I have an iframe. 我有一个iframe。 I want to access a table which present inside iframe.I want to access it from parent window of iframe. 我想访问iframe中存在的表。我想从iframe的父窗口访问它。 I have written JS like 我写过像

Document.getElementById("table Id");

But the result come null. 但是结果为空。 What is the process to get it? 得到它的过程是什么? thanks 谢谢


x=document.getElementById("iFrameId");
x.contentDocument.getElementById("tableId");

if you can use jQuery i guess it will work across browsers 如果您可以使用jQuery,我想它会在所有浏览器中正常工作

$("#iFrameId").contents().find("#tableId")

You have to select the element from the iFrame's document. 您必须从iFrame的文档中选择元素。 Per Juan's comment, check against both the name, and id of the iFrame 根据Juan的评论,同时检查iFrame的名称和ID

var targetFrame = window.frames["nameOfIFrame"] || window.frames["iFrameId"];
targetFrame.document.getElementById("tableId");

EDIT 编辑

I just tested this with the following: 我只是用以下方法对此进行了测试:

window.frames["fName"].document.getElementById("Adam").innerHTML

in the Chrome console, and it output the html of the Adam div from within my iframe. 在Chrome控制台中,它从我的iframe中输出Adam div的html。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM