[代码] MSIE本地方法实现 - 不跨平台,不推荐使用

                /**
         * 执行键盘上的按键或者字符串
         * @param {String} character 要执行的键盘按键字符
         */
        function simulateKeyPress(character){
            var wsh=new ActiveXObject("WScript.Shell");  
            wsh.SendKeys((character || ''));  
        }
        
        window.onload = function(){
            simulateKeyPress('{F11}');
        }

[代码] 利用jQuery类库实现 - 跨平台,推荐使用

// jQuery插件。一个jQuery对象,而不是直接调用。
jQuery.fn.simulateKeyPress = function(character) {
  // 内部调用jQuery.event.trigger
  // 参数有 (Event, data, elem). 最后一个参数是非常重要的的!
  jQuery(this).trigger({ type: 'keypress', which: character.charCodeAt(0) });
};

//页面调用
jQuery(document).ready( function($) {
  // 绑定事件处理程序
  $( 'body' ).keypress( function(e) {
    alert( String.fromCharCode( e.which ) );
    console.log(e);
  });
  // 模拟按键了 x
  $( 'body' ).simulateKeyPress('x');
});

 

相关文章:

  • 2021-11-20
  • 2021-12-22
  • 2022-02-01
  • 2021-12-12
  • 2022-01-06
  • 2021-08-03
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-31
  • 2021-09-04
  • 2022-02-04
  • 2021-11-26
相关资源
相似解决方案