如何模拟点击

发布于 2024-08-10 08:58:55 字数 533 浏览 6 评论 0原文

当我们使用这种类型的输入提交表单时,坐标 (x,y) 会附加到结果中。

如果我动态创建 2 个隐藏输入,它会起作用,但我想要不同的东西。
Event 或 MouseEvent 会很棒,但我无法让它工作,

这是我当前的代码:(它工作正常)

var input = document.createElement("input");
input.type = "hidden";
input.name = "x";
input.value = posx;
my_form.appendChild(input);

input = input.cloneNode(false);
input.name = "y";
input.value = posy;
my_form.appendChild(input);

我会给你一个情况示例

让我们想象一下,在图像表单元素附近有一个纯文本,上面写着:点击下图中大于20小于60的任意位置

正常人会读到此内容并正常点击。
但我需要作为一个没有手的机器人,模拟相同的点击事件

when we submit a form using this type of input, the coordinates (x,y) are appended to the result.

it works if I dynamically create 2 hidden inputs, but I wanted something different.
Event or MouseEvent would be great, but I couldn't make it work

here's my current code: (it works fine)

var input = document.createElement("input");
input.type = "hidden";
input.name = "x";
input.value = posx;
my_form.appendChild(input);

input = input.cloneNode(false);
input.name = "y";
input.value = posy;
my_form.appendChild(input);

I'll give you an example of situation

Let's imagine that near to the image form element there is a plain text that says: Click in the image below in any position greater than 20 and lesser than 60

A normal person would read this and click normally.
But I need to, as a robot without hands, simulate the same click event

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

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

发布评论

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

评论(2

夜深人未静 2024-08-17 08:58:55

在没有完全理解你在这里想要做什么的情况下(你的问题有点模糊,IMO),我不得不问这个:你有没有考虑过使用 jQuery 或其他 javascript 库/框架?我知道有时候,对于非常简单的网站来说,这是矫枉过正的。但是,为了减轻自己解决这些问题的头痛可能是值得的。

据我所知,您正在使用“图像”表单元素来提交表单,并且您想要模拟单击图像元素来检索某些内容的 x/y 坐标。我可能是错的。你能更明确地描述你的细节吗?

当我更多地了解你的问题时,我会看看是否可以向你展示如何在 jQuery 中做到这一点。

Without fully understanding what you are trying to do here (your question is a little vague, IMO), I have to ask this: Have you ever considered using jQuery or another javascript library/framework? I understand sometimes, for very simple sites, it's overkill. But, it might be worth it to relieve the headache of figuring this stuff out on your own.

From what I can understand, you are using an "image" form element to submit a form and you want to simulate a click on the image element to retrieve the x/y coordinates of something. I could be wrong. Could you be more explicit in your details?

I'll see if I can show you how to do it in jQuery when I know more about your problem.

触ぅ动初心 2024-08-17 08:58:55

我不知道你想将结果附加到什么。表格中的值?这是自动的,不需要任何代码(它们会遇到 x 和 y,或者 elementname.x 和 elementname.y)。

评论后编辑:
您可以通过当时不创建元素来避免节点插入事件;即在表格中添加 x 和 y。然后在事件中您可以简单地设置它们的值。当然,您也可以绑定不同的事件,我不知道您的情况,但我做了 使用附加到页面本身主体的 onClick 事件的示例。相反,您可能希望将特定的 onClick 事件绑定到不同的可点击项目,并为它们插入您自己的位置,但在我的例子中,我提交了鼠标在页面上的位置。如果这没有帮助,请评论原因,我会看看是否可以再次修改它。

I can't tell what you're trying to append the result to. The values in the form? This is automatic and requires no code (they come across and x and y, or elementname.x and elementname.y).

edited after comment:
You can avoid the node insert event by not making the elements at that time; ie add x and y to the form. Then on the event you can simply set their value. There are certainly different events you could bind this too, and I don't know your situation, but I made an example that uses an onClick event attached to the body of the page itself. You may want, instead, to bind specific onClick events to different clickable items and insert your own locations for them but in my case I submitted the location of the mouse on the page. If that's not helpful, make a comment about why and I'll see if I can mod it again.

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