document.elementFromPoint在jQuery中不工作
我正在尝试创建一个jQuery脚本,以确定所需元素在给定坐标下是否可见(而不是对其他元素隐藏) 为此,我尝试使用document.elementFromPoint在jQuery中不工作,jquery,dom,visibility,Jquery,Dom,Visibility,我正在尝试创建一个jQuery脚本,以确定所需元素在给定坐标下是否可见(而不是对其他元素隐藏) 为此,我尝试使用document.elementFromPoint函数,但它似乎不起作用(请参阅) JS var test = $("#test"); var debug = $("#debug"); // "test" is larger than 100x100 px, so I suppose to find it here if(document.elementFromPoint(100,1
document.elementFromPoint
函数,但它似乎不起作用(请参阅)
JS
var test = $("#test");
var debug = $("#debug");
// "test" is larger than 100x100 px, so I suppose to find it here
if(document.elementFromPoint(100,100) === test) {
debug.html("found!");
}
else {
debug.html("not found");
}
我缺少什么?返回,因此需要获取DOM元素而不是jQuery对象:
if(document.elementFromPoint(100,100) === test[0]) {
debug.html("found!");
}
else {
debug.html("not found");
}
另一种方法是使用is
jQuery方法:
从调用elementFromPoint方法的文档返回元素,该元素是位于给定点下的最顶层元素。要获取元素,请通过坐标指定点,以CSS像素为单位,相对于包含文档的窗口或框架中最左上角的点
描述:对照选择器、元素或jQuery对象检查当前匹配的元素集,如果这些元素中至少有一个与给定参数匹配,则返回true
- 元素
类型:
与当前元素集匹配的一个或多个元素元素
var测试=$(“#测试”);
var debug=$(“#debug”);
if(document.elementFromPoint(100100)==test[0]){
html(“找到了!”);
}否则{
html(“未找到”);
}
#测试{
显示:内联块;
宽度:200px;
高度:200px;
背景颜色:绿色;
}
这很简单:您正在将jQuery
的实例与htmldevelment
的实例进行比较。要么使用IonicăBizău的答案,要么放弃jQuery,改用document.getElementById()
。
if(test.is(document.elementFromPoint(100,100))) {
/* found */
}
else {
/* not found */
}