检索Ruby中HTML元素的位置(x,y)

发布于 2025-02-01 10:04:00 字数 224 浏览 5 评论 0原文

我有一个示例显示了用户的一些任意文本:

<html>
 <head></head>
  <body>
     <%= @article.text %>    
  </body>
</html>

我想知道如何获得html元素的x和y位置,例如使用ruby等 @actits.text。

I have this example that displays some arbitrary text from the user:

<html>
 <head></head>
  <body>
     <%= @article.text %>    
  </body>
</html>

I want to know how to get the X and Y position of HTML elements such as @article.text using Ruby.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋 2025-02-08 10:04:00

将一些ID设置为元素

<html>
<head></head>
<body>
  <div id="my-id">
    <%= @article.text %>
  </div>
</body>
</html>

并使用getBoundingClientRect

const getCoordinates = (element) => {
  const box = element.getBoundingClientRect()

  return {
    top: box.top + window.pageYOffset,
    right: box.right + window.pageXOffset,
    bottom: box.bottom + window.pageYOffset,
    left: box.left + window.pageXOffset
  }
}

const element = document.getElementById('my-id')

const coordinates = getCoordinates(element)

console.log(coordinates)

参考:

https:/ https://javascript.info /坐标

Set some id to element

<html>
<head></head>
<body>
  <div id="my-id">
    <%= @article.text %>
  </div>
</body>
</html>

And get position in JS with getBoundingClientRect

const getCoordinates = (element) => {
  const box = element.getBoundingClientRect()

  return {
    top: box.top + window.pageYOffset,
    right: box.right + window.pageXOffset,
    bottom: box.bottom + window.pageYOffset,
    left: box.left + window.pageXOffset
  }
}

const element = document.getElementById('my-id')

const coordinates = getCoordinates(element)

console.log(coordinates)

Reference:

https://javascript.info/coordinates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文