How do you get a <div>
from within an <iframe>
?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
Do not forget to access iframe after it is loaded. Old but reliable way without jQuery:
Below code will help you to find out iframe data.
None of the other answers were working for me. I ended up creating a function within my iframe that returns the object I was looking for:
Then you call that function like so to retrieve the element:
You could more simply write:
and the first valid inner doc will be returned.
Once you get the inner doc, you can just access its internals the same way as you would access any element on your current page. (
innerDoc.getElementById
...etc.)IMPORTANT: Make sure that the iframe is on the same domain, otherwise you can't get access to its internals. That would be cross-site scripting.
CallYourFunction()
is function inside page and that function action on itAbove answers gave good solutions using Javscript. Here is a simple jQuery solution:
The trick here is jQuery's
.contents()
method, unlike.children()
which can only get HTML elements,.contents()
can get both text nodes and HTML elements. That's why one can get document contents of an iframe by using it.Further reading about jQuery
.contents()
: .contents()Note that the iframe and page have to be on the same domain.