简体   繁体   English

如何在jQuery / Javascript中逐个获取

[英]How to get element by point in jQuery/Javascript

I want to get an element by specifying the point values in page. 我想通过在页面中指定点值来获取元素。

var point = this.svgRenderer.getPoint(serPoint, chart);

This will return exact point location of the page. 这将返回页面的精确点位置。 That point contains some SVG element either circle or rectangle or image or other elements. 该点包含一些SVG元素,可以是圆形或矩形,也可以是图像或其他元素。 I want to get the element based on the point location. 我想根据点位置获取元素。 Is this possible? 这可能吗?

why don't you just use document.elementFromPoint . 为什么不使用document.elementFromPoint

the example in the link below shows how it works . 以下链接中的示例显示了它的工作原理。

get element by point 一点一点地得到

Maybe you can try getIntersectionList from SVG-DOM using a rectangle with a height and width of one as parameter: 也许您可以使用高度和宽度为1的矩形作为参数从SVG-DOM尝试getIntersectionList:

Full example: 完整示例:

<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100" height="100" viewBox="0 0 100 100" onclick="elementsAtEventPosition(event)">
    <script>
        function elementsAtEventPosition(event) {
            var svgRootNode = document.documentElement;
            var rect = svgRootNode.createSVGRect();
            rect.x = event.clientX;
            rect.y = event.clientY;
            rect.width = 1;
            rect.height = 1;
            alert(svgRootNode.getIntersectionList(rect, svgRootNode).length);
        }
    </script>
    <rect x="10" y="10" width="50" height="50" fill="black" stroke="black" stroke-width="1" />
</svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM