【问题标题】:Disable Ctrl-A (Cmd-A) text selection in Firefox在 Firefox 中禁用 Ctrl-A (Cmd-A) 文本选择
【发布时间】:2014-04-04 02:36:31
【问题描述】:

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 中(没有版权、注销链接、菜单等)。

【问题讨论】:

  • 我希望你不要试图阻止文本的复制,因为无论哪种方式都是徒劳的。
  • 不,只有选择。这只是视觉上的东西。我已经为此编辑了问题并添加了此评论。

标签: css firefox selection


【解决方案1】:

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";
}

【讨论】:

  • 查看已编辑的问题(最后)。对于“基于包含的元素选择”(即在任何地方禁用选择但仅在某些元素上启用它),这看起来像是一个非常昂贵的选项(性能)。我说的对吗?
【解决方案2】:

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

【讨论】:

    【解决方案3】:
    $(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;
    }
    

    Documentation

    【讨论】:

      【解决方案4】:

      试试这个:

      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/

      【讨论】:

      • 可能是一个组合:jsfiddle.net/9ytfe/14 但仍然不完整。在 Firefox 中,您可以右键单击 -> 全选或双击。这些仍然会选择文本。所以可能更接近使用onselectstart 的解决方案会更好。
      猜你喜欢
      • 1970-01-01
      • 2013-01-13
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 2011-01-25
      • 2019-04-10
      相关资源
      最近更新 更多