问题描述
所以我知道这听起来像一个重复,但它不是(或者如果它是,所有的接受答案,我可以找到不工作,我需要它的方式)。问题是这样的:
So I know this sounds like a duplicate, but it isn't (or if it is, the accepted answer on all the ones I can find doesn't work the way I need it to). The issue is this:
我使用jQuery编写HTML5,我需要做一个网格允许多选择与控制和移位。我有这个逻辑工作,但每当你点击它选择网格中的文本。我想阻止这种选择,但这里和我发现的其他问题之间的关键区别:我想要选择的文字在所有其他时间工作。
I'm writing in HTML5 using jQuery, I need to make a grid allow multi-select with control and shift. I have that logic working, but whenever you shift-click it selects the text in the grid. I want to prevent this selection, but here's the critical difference between this and the other questions I found: I want selection of text to work at all other times.
重述:我要禁用使用shift WITHOUT 禁用所选文本选项的所有文本选择。有人知道我该怎么做?
To restate: I want to disable text selection using shift WITHOUT disabling all text selection for the elements specified. Does anyone know how I can do that?
- EDIT -
以下(在网格的构造函数中)解决了这个问题我。正如回答者建议的,我宣布了一个不可选择的类。
-- EDIT --
The following (in the constructor for the grid) solved this for me. As the answerer suggested, I declared a class for unselectability.
this.gridBody = $("#userGrid");
var handleKeydown = function(e)
{
e = e || window.event;
var keyPressed = e.keyCode || e.which;
if (keyPressed == keys.shift) {
e.data.gridBody.addClass("unselectable");
}
};
var handleKeyup = function(e)
{
e = e || window.event;
var keyPressed = e.keyCode || e.which;
if (keyPressed == keys.shift) {
e.data.gridBody.removeClass("unselectable");
}
};
$(document).on('keydown', this, handleKeydown);
$(document).on('keyup', this, handleKeyup);
推荐答案
按下向下键选择
That will bind on document an event where it disables text selection upon pressing DOWN shift
document.onkeydown = function(e) {
var keyPressed = e.keyCode;
if (keyPressed == 16) { //thats the keycode for shift
$('html').css({'-moz-user-select':'-moz-none',
'-moz-user-select':'none',
'-o-user-select':'none',
'-khtml-user-select':'none',
'-webkit-user-select':'none',
'-ms-user-select':'none',
'user-select':'none'
}); //or you could pass these css rules into a class and add that class to html instead
document.onkeyup = function() {
//here you remove css rules that disable text selection
}
}
}
希望我帮了你。
document.onkeydown = function(e) {
var keyPressed = e.keyCode;
if (keyPressed == 16) { //thats the keycode for shift
$('html').addClass('unselectable'); //unselectable contains aforementioned css rules
document.onkeyup = function() {
$('html').removeClass('unselectable'); //and simply remove unselectable class making text selection availabe
}
}
}
这篇关于如何在不禁用所有文本选择的情况下使用shift禁用文本选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!