JavaScript(jQuery)事件触发器ctrl +右箭头按钮

时间:2016-08-12 08:30:32

标签: javascript jquery

我需要模仿按组合按钮 Ctrl + 我怎么能在页面加载后运行触发哪个 将是组合: Ctrl + 我找到这个代码

$("input").focus();
var e = jQuery.Event("keydown");
e.which = 75; // # Some key code value
$("input").val(String.fromCharCode(e.which));
$("input").trigger(e);

但我不明白我可以如何插入

e.which = 75; // # Some key code value

e.which = ctrl code + right arrow code; // # Some key code value

1 个答案:

答案 0 :(得分:1)

右箭头的键码是39;您可以通过挂钩并查看which获得真实事件的价值来轻松确定。

要指示Ctrl键已关闭,请在事件中将ctrlKey设置为true

在关注输入之后,您可能希望使用setTimeout(..., 0)技巧为浏览器提供实际操作的机会。

所以:



setTimeout(function() {
    $("input").focus();
    setTimeout(function() {
        var e = jQuery.Event("keydown");
        e.which = 39; // 39 = right-arrow
        e.ctrlKey = true;
        $("input").trigger(e);
    }, 0);
}, 300);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<input type="text" value="one two three">
&#13;
&#13;
&#13;

也就是说,它实际上并没有在Chrome上执行操作,如果它也不在其他浏览器上,我也不会感到惊讶。

相关问题