Can I access to iframe 's parent's element

2019-02-15 18:11发布

问题:

First the two page in under the same domain

In the parent page.htm I have only a element like input#test,

then I set a iframe in the parent page,called child.htm,in the child.html I write js to find the parent input element,like

var parent = window.self.top;

but I can't use var input = $(parent).find('input#test') to find the input element in parent.htm

Can I access to it? How can I achieve that?

回答1:

Try

var input = $(window.self.top).contents().find('input#test');

or

var input = $(window.self.top.document).find('input#test');