2

在这个jsFiddle中,您不能在 Firefox 中使用鼠标选择文本。但这仍然可以使用 Cmd-A 或 Ctrl-A 键序列。有没有办法在 Firefox 中禁用它?

我正在使用这个 CSS 类:

.noselect {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

我试图这样做不是为了阻止文本复制,而只是为了让 Ctrl-A + Ctrl-C 在某些元素上工作以增强用户体验。就像,您登陆一个页面,将其复制并粘贴到 Excel 中。只有有用的信息才应该出现在 Excel 中(没有版权、注销链接、菜单等)。

4

4 回答 4

0
$(function(){   
    $(document).keydown(function(objEvent) {        
        if (objEvent.ctrlKey) {          
            if (objEvent.keyCode == 65) {                         
                objEvent.disableTextSelect();
                return false;
            }            
        }        
    });
}); 

65 是 'A' 的 ASCII 码,如果还要检查 'a',请添加 97。

你可以用 CSS 做到这一点:

div {
    user-select:none;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
}

文档

于 2013-08-07T10:33:14.537 回答
0

看起来在 Firefox 中它只是一个视觉的东西(错误)。按下 Ctrl-A 时文本会被选中,但按下 Ctrl-C 时不会复制。

于 2013-08-07T10:25:34.300 回答
0

谷歌是你的朋友。

/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

function disableSelection(target) {
  if (typeof target.onselectstart != "undefined") //IE route
    target.onselectstart = function() { return false; }
  else if (typeof target.style.MozUserSelect != "undefined") //Firefox route
    target.style.MozUserSelect = "none"
  else //All other route (ie: Opera)
    target.onmousedown = function() { return false; }
  target.style.cursor = "default";
}

//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"

修复了上面的代码:http: //jsfiddle.net/jUfe4/7/

function disableSelection(target) {
    var ieSelect = typeof target.onselectstart != "undefined";
    var ffSelect = typeof target.style.MozUserSelect != "undefined"

    if (ieSelect) target.onselectstart = function () { return false; };
    else if (ffSelect) target.style.MozUserSelect = "none"
    else target.onmousedown = function () { return false; };

    target.style.cursor = "default";
}
于 2013-08-07T10:04:44.833 回答
0

尝试这个:

var isCtrl = false,
    isCmd  = false;

$(document).keyup(function (e) {

 if(e.ctrlKey)
     isCtrl=false;
     isCmd  = false;

}).keydown(function (e) {
    if(e.ctrlKey) 
        isCtrl = true;
        isCmd = true;

    if(e.which == 65 && isCtrl == true) {
        console.log('ctrl+a');    
        return false;
    }

    if(e.which == 65 && isCmd == true) {
        console.log('cmd+a');    
        return false;
    }
});

演示:http: //jsfiddle.net/9ytfe/13/

于 2013-08-07T10:27:46.387 回答