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");