在Chrome上使用Jquery检测shift +点击 [英] Detecting a shift + click with Jquery on Chrome
本文介绍了在Chrome上使用Jquery检测shift +点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用javascript检测转换点击,但由于某种原因,它仅适用于IE
I'm trying to detect a shift click with javascript but for some reason it only works on IE
.click(function (e) {
if (e.shiftKey) {
Rain();
}
});
这是我在IE中使用的代码,我如何在Chrome上检测它
this is the code that work for me in IE, how can I detect it on Chrome
推荐答案
我不认为有一个定义的组合,但你可以自己做。一个(粗略的)例子:
I don't think there is a defined combo, but you could make it yourself. A (crude) example:
<div id="someElement">
Click me for an alert!
</div>
<script>
var shiftPressed = false;
$(document).keydown(function(event) {
shiftPressed = event.keyCode==16;
});
$(document).keyup(function(event) {
if( event.keyCode==16 ){ shiftPressed = false; }
});
$('#someElement').on('click', function(e){
if( shiftPressed ){
alert("Shift and click!");
}
else{ alert("Nope"); }
});
</script>
只需绑定 .keyup()
当keydown是为了最大限度地减少事件数量的转变。你应该在if语句之外添加尽可能少的逻辑,因为这个事件被触发很多
You could improve it by only binding the .keyup()
when the keydown is a shift in order to minimize the number of events. You should add as little logic as possible outside the if statements, as this event gets fired a lot
这篇关于在Chrome上使用Jquery检测shift +点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文