在此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(没有版权,注销链接,菜单等)。
答案 0 :(得分:0)
Google是你的朋友。
/***********************************************
* 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";
}
答案 1 :(得分:0)
在Firefox中看起来它只是一个 visual 的东西(bug)。按Ctrl-A时会选择文本,但按Ctrl-C时会复制 NOT 。
答案 2 :(得分: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;
}
});
答案 3 :(得分: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;
}