模拟按住Ctrl键 [英] Simulate Holding Ctrl Key

查看:152
本文介绍了模拟按住Ctrl键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改多重选择元素的默认行为,以便用户可以选择并取消选择多个值,而无需一直按下 Ctrl 键。



我发现一个简单的解决方案这里,但是这在ie8中不起作用(因为在ie中, onmousedown 不适用于选项元素)。



但是我想,只要鼠标悬停在多重选择上,可以模拟一个按下的控制键:

  $(document).ready(function(){
$('select')。hover(function(e){
var kde = jQuery.Event (keydown);
kde.ctrlKey = true; //这样的东西
kde.keyCode = 17; //或者 - 我不知道
$(e.target ).trigger(kde);
});
});

为什么这不起作用?




  • 是否直接再次发布 Ctrl 键?

  • 代码有问题吗?

  • <我完全没有其他的东西吗?

解决方案

以编程方式推动键盘按钮,就像用户在键盘上推送一个键时,通过模拟 shift 键不能产生大写A。此外,即使它将工作不起作用:在Mac上,您按 cmd 而不是 ctrl 选择多个元素。 p>

所以不幸的是,你必须删除这种方法,并寻找其他选项。


I'm currently trying to change the default behaviour of a multiselect element, so that a user can select and deselect multiple values, without having to press the Ctrl key all the while.

I found a simple solution here, but this does not work in ie8 (because in ie, the onmousedown does not apply to option elements).

But I figured, that one could just simulate a pressed control key, whenever the mouse hovers over a multiselect:

$(document).ready(function() {
    $('select').hover(function(e) {
        var kde = jQuery.Event("keydown");
        kde.ctrlKey = true;  //something like this
        kde.keyCode = 17;    //or this - i don't know
        $(e.target).trigger(kde);        
    });
});

Why does this not work?

  • Is the Ctrl key directly being released again?
  • Is something wrong with the code?
  • Am I missing something else entirely?

解决方案

You can't simulate such events by programmatically pushing keyboard buttons, just like you can't produce a capital A by simulating the shift key while the user pushes the a key on their keyboard. Besides, even if it would work it wouldn't work: on Macs you press cmd, not ctrl, to select multiple elements.

So unfortunately you'll have to drop this approach and look for other options.

这篇关于模拟按住Ctrl键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆