在Javascript中模拟鼠标单击

在Javascript中模拟鼠标单击

本文介绍了在Javascript中模拟鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找在ID或CLASS名称标识的图像/按钮上单击鼠标左键的Javascript,等待x秒并重复。并且能够在开发人员工具中运行控制台tap,在chrome和firefox中。

Looking for a Javascript that do a left-mouse click on a image/button identified by ID or CLASS name, wait x seconds and repeat. And able to run in developer tools Console tap, in chrome and firefox.

尝试自己写,因为我认为这将是一个简单的代码,但2小时后没有运气的试验和错误,我开始没有选择。

Tried to write it myself, cause i figured it would be a simple code, but after 2 hours of trial and error with no luck, i am starting to run out of options.

希望Javascript专家有时间帮助一个非常新手的用户;)

Hopefully a Javascript pro out there got time to help out a very novice user ;)

谢谢

推荐答案

你要使用和 dispatchEvent 为此:

var support = true; // check if event constructor is supported

try {
    if (new MouseEvent('click', {bubbles: false}).bubbles !== false) {
        support = false;
    } else if (new MouseEvent('click', {bubbles: true}).bubbles !== true) {
        support = false;
    }
} catch (e) {
    support = false;
}

setInterval(function() {
    if (support) {
        var event = new MouseEvent('click');
    }else{
        var event = document.createEvent('Event');
        event.initEvent('click', true, true);
    }
    elem.dispatchEvent(event);
},1000);

这篇关于在Javascript中模拟鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:22