简体   繁体   English

什么可能阻止我的 javascript click() function?

[英]What could be blocking my javascript click() function?

I am attempting to write a script to automate some tasks on a third party site.我正在尝试编写一个脚本来自动化第三方网站上的一些任务。 The very first step is simply clicking a div on the nav, but document.getElementById('myId').click() does nothing.第一步是简单地单击导航上的 div,但document.getElementById('myId').click()什么也不做。

In my searching I found this answer that fully simulates a mouse click: Simulating a mousedown, click, mouseup sequence in Tampermonkey?在我的搜索中,我发现了这个完全模拟鼠标点击的答案: Simulating a mousedown, click, mouseup sequence in Tampermonkey?

However, that also does not work.但是,这也行不通。 I did notice that there's a class added when hovering, and the script successfully simulates that.我确实注意到悬停时添加了一个 class ,并且脚本成功地模拟了它。 And obviously physically clicking works fine.显然,物理点击效果很好。 I'm not sure what else I could be missing我不确定我还能错过什么

Edit: It turns out that the clicking was just fine, but the site is actually checking pieces of the event such as the coordinates, which is why it appeared to not be functioning properly编辑:事实证明点击很好,但该站点实际上正在检查事件的各个部分,例如坐标,这就是它似乎无法正常运行的原因

If I understand correctly, what you want is this:如果我理解正确,你想要的是:

document.getElementById('myId').addEventListener('click', myFunction);

This will run the function myFunction() when the element with the ID of "#myId" is clicked.当单击 ID 为“#myId”的元素时,这将运行 function myFunction()。

Hope this helps!希望这可以帮助!

PS If this doesn't work, I suggest using Shashank Bhatt's suggestion of checking pointer-events. PS 如果这不起作用,我建议使用 Shashank Bhatt 的检查指针事件的建议。

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

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