我需要模仿按下组合按钮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
最佳答案
右箭头的键码是39;您可以通过 Hook 它并查看真实事件的 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">
也就是说,它实际上并不在 Chrome 上执行该操作,如果它在其他浏览器上也不执行,我也不会感到惊讶。
关于JavaScript(jQuery)事件触发ctrl+右箭头按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38913401/