简体   繁体   English

如何模拟对元素的单击,这也会导致调用附加事件?

[英]How to simulate a click on a element which also results in invoking attached events?

My test case is @ Gmail, I am trying to simulate a click to open the first e-mail with JavaScript. 我的测试用例是@ Gmail,我正在尝试模拟单击以使用JavaScript打开第一封电子邮件。

Actually I did a successful attempt which is done by firing a mousedown event. 实际上,我通过触发mousedown事件进行了成功的尝试。

But I wonder if there is a generic method to generate "organic" clicks instead of me wasting time on investigating attached events ? 但是我想知道是否有一种通用的方法来产生“有机”点击,而不是浪费我的时间来调查附加事件?

You can just trigger the usual suspects...but other than that, there's no good generic way to simulate a native click from a user (and an actual native click is impossible, for example making an <a> go to it href , without doing the redirect yourself). 您可以触发通常的嫌疑人...但是除此之外,没有一种很好的通用方法来模拟用户的自然点击(而且实际的自然点击是不可能的,例如使<a>转到href ,而没有自己进行重定向)。

Triggering the relevant events to a click for something like gmail would look like this: 触发类似gmail之类的click的相关事件如下所示:

$(selector).mousedown().mouseup().click();

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

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