【问题标题】:jquery custom keycode trigger not workingjquery自定义键码触发器不起作用
【发布时间】:2011-10-12 20:32:02
【问题描述】:

我想像这样在所选元素上发送击键:

$(":input").click(function(){
    $(this).trigger("keydown", [{
        preventDefault:function(){},
        keyCode:9,
        shiftKey: true
    }]);
});

//wants to send Shift+Tab keystroke on input click

演示:http://jsfiddle.net/NYwCT/.

【问题讨论】:

  • 那么问题出在哪里??您能否澄清一下您想要什么以及目前正在发生什么?

标签: javascript jquery triggers keypress keystroke


【解决方案1】:

您不能发送击键。它目前会触发任何绑定的函数,但不会在浏览器级别执行相关的事情(发送 ALT+F4 的脚本呢?)。

如果你想在点击之前聚焦输入,你可以使用.prev()(前一个兄弟):http://jsfiddle.net/NYwCT/1/

$(":input").click(function(){
    $(this).prev().focus();
});

【讨论】:

    【解决方案2】:

    这不会触发实际事件

    var press = jQuery.Event("keypress");
    press.shiftKey = true;
    press.keyCode = 9;
    $(this).trigger(press);
    

    你也可以试试这个插件:fn.sendkeys or jwerty

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多