How to access element present inside a iframe from

2019-02-21 01:36发布

问题:

I have an iframe. I want to access a table which present inside iframe.I want to access it from parent window of iframe. I have written JS like

Document.getElementById("table Id");

But the result come null. What is the process to get it? thanks

回答1:


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

if you can use jQuery i guess it will work across browsers

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



回答2:

You have to select the element from the iFrame's document. Per Juan's comment, check against both the name, and id of the iFrame

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.