document.elementFromPoint(x,y)在iframe中获取元素 [英] document.elementFromPoint(x,y) to get element inside iframe

查看:1308
本文介绍了document.elementFromPoint(x,y)在iframe中获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在html页面中获取元素,我使用document.elementFromPoint(x,y)来检测输入元素;当没有iframe时它工作正常。但在内部iframe中,它在这段代码中不起作用。



html如下



我错过了什么吗? / p>

 < html> 
...
< div>
< div>
< iframe src =some source..>
< html>
..
< form>
< fieldset>
<输入>

谢谢

解决方案 div>

根据这里的文档:



如果指定点上的元素属于另一个文档(例如,iframe的子文档),则调用该方法的文档DOM中的元素(在iframe的情况下,



这意味着如果您当前的DOM上下文是iframe的父项,它应该返回iframe。另外还有跨域安全问题,如果iframe来自其他域,则应详细阅读。



如果它来自您的域,并且您希望获取iframe中的元素,您可以执行以下操作:

  var el = document.elementFromPoint(x,y); 
if(el instanceof HTMLIFrameElement)
el = el.contentWindow.document.elementFromPoint(x,y); //不知道是否需要更新x,y来说明是否在另一个dom中。


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

解决方案

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.

这篇关于document.elementFromPoint(x,y)在iframe中获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆