admin管理员组

文章数量:1384229

I am trying to get elements in html page, i use document.elementFromPoint(x,y) to detect input elements; it works fine when there are no iframes. But inside iframes it does not work inside this code

the html as follow

Am I missihng something?

<html>
...
<div>
<div>
<iframe src="some source"..>  
<html>
..
<form>
<fieldset>
<input>

Thanks

I am trying to get elements in html page, i use document.elementFromPoint(x,y) to detect input elements; it works fine when there are no iframes. But inside iframes it does not work inside this code

the html as follow

Am I missihng something?

<html>
...
<div>
<div>
<iframe src="some source"..>  
<html>
..
<form>
<fieldset>
<input>

Thanks

Share Improve this question edited Jan 10, 2012 at 22:11 Anass Kartit asked Jan 10, 2012 at 22:10 Anass KartitAnass Kartit 2,09815 silver badges26 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

As per the documentation here:

"If the element at the specified point belongs to another document (for example, an iframe's subdocument), the element in the DOM of the document the method is called on (in the iframe case, the iframe itself) is returned."

Meaning that it should return the iframe if your current DOM context is the iframe's parent. Also there are cross domain security issues you should read up on if the iframe is from another domain.

If it is from your domain and you wish to get the element inside of the iframe you would do the following:

var el = document.elementFromPoint(x, y);
if (el instanceof HTMLIFrameElement)
    el = el.contentWindow.document.elementFromPoint(x, y);  //Not sure if you need to update x, y to account for being inside another dom.

If your iframe is not on the same domain as your website, you won't be able to select tags using jquery/javascript

本文标签: javascriptdocumentelementFromPoint(xy) to get element inside iframeStack Overflow