JQuery或JavaScript:如何在单击锚标签超链接时确定是否按下了shift键? [英] JQuery or JavaScript: How determine if shift key being pressed while clicking anchor tag hyperlink?
本文介绍了JQuery或JavaScript:如何在单击锚标签超链接时确定是否按下了shift键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个调用JavaScript函数的锚标记。
I have an anchor tag that calls a JavaScript function.
无论有没有JQuery,如何在点击链接时确定shift键是否关闭?
With or without JQuery how do I determine if the shift key is down while the link is clicked?
以下代码不起作用,因为只有按下真实键(不是shift键)才会触发按键。 (我希望如果仅按下shift键就会触发。)
The following code does NOT work because keypress is only fired if a "real key" (not the shift key) is pressed. (I was hoping it would fire if just the shift key alone was pressed.)
var shifted = false;
$(function() {
$(document).keypress(function(e) {
shifted = e.shiftKey;
alert('shiftkey='+e.shiftkey);
});
$(document).keyup(function(e) {
shifted = false;
});
}
...
function myfunction() {
//shift is always false b/c keypress not fired above
}
推荐答案
$(document).on('keyup keydown', function(e){shifted = e.shiftKey} );
这篇关于JQuery或JavaScript:如何在单击锚标签超链接时确定是否按下了shift键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文