jQuery:绑定" CTRL + A"仅选择特定区域

时间:2015-03-14 04:50:51

标签: javascript jquery select

我要做的是,当按下CTRL + A时,选择全部的快捷方式;我不希望它选择除了特定区域之外的所有东西(就像带有xyz类的容器内的所有东西)。但是,我无法正常工作。即使只使用select()也无效。例如:

$("table").select();

我尝试使用我在这里找到的js解决方案,但如果某个区域处于活动状态并且我无法解决它,它就会绑定它。这是代码:

$(document).keydown(function(e) {
    if (e.keyCode == 65 && e.ctrlKey) {
        var range = document.createRange();
        range.selectNode(document.getElementById('table'));
        window.getSelection().addRange(range);
        e.preventDefault();
    }
});

我在这里创建了一个jsfiddle:http://jsfiddle.net/zpd8yLn7/

我想要的是,只要按下CTRL + A,就会选择class="class"内的文字。

1 个答案:

答案 0 :(得分:2)

如果您对纯粹的css解决方案持开放态度:JS Fiddle

body > :not(.class) {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

参考文献:

CSS rule to disable text selection highlighting

CSS-Tricks - not(s)