jQuery:将“CTRL+A”绑定为仅选择特定区域

jQuery: Binding "CTRL+A" to only select specific area only

本文关键字:选择 区域 绑定 CTRL+A jQuery      更新时间:2023-09-26

我想做的是,当按下 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"选择里面的文本。

如果你对纯粹的css解决方案持开放态度:JS小提琴

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 规则

CSS 技巧 - 不是