gpt4 book ai didi

javascript - document.elementFromPoint(x,y) 获取 iframe 内的元素

转载 作者:太空宇宙 更新时间:2023-11-04 15:36:01 27 4
gpt4 key购买 nike

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

html如下

我是不是漏掉了什么?

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

谢谢

最佳答案

根据文档 here :

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

意味着如果您当前的 DOM 上下文是 iframe 的父级,它应该返回 iframe。还有cross domain security issues如果 iframe 来自另一个域,您应该仔细阅读。

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

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.

关于javascript - document.elementFromPoint(x,y) 获取 iframe 内的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8811264/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com