【前端】js代码模拟用户键盘鼠标输入

2023-10-17 09:54:25 浏览数 (1)

js代码模拟用户键盘鼠标输入

原生js

代码语言:javascript复制
var event = new Event('mousewheel');
event.wheelDelta = 360
document.dispatchEvent(event);

jquery

代码语言:javascript复制
function simulateKeyPress(character) {
    // 方法1
    jQuery.event.trigger({ 
        type : 'keypress', 
        which : character.charCodeAt(0) 
    });

    // 方法2
    var event = jQuery.Event( "logged" );
    event.user = "foo";
    event.pass = "bar";
    $( "body" ).trigger( event );

    // 方法3
    $( "body" ).trigger({
        type:"logged",
        user:"foo",
        pass:"bar"
    }); 
}

$('body').keypress(function(e) {
    alert(e.which);
});

simulateKeyPress("e");

0 人点赞