简体   繁体   English

禁用选择,允许复制/粘贴

[英]Disable select, allow copy/paste

I'm building an app which overrides standard selecting behaviour and allow copying and pasting elements. 我正在构建一个覆盖标准选择行为并允许复制和粘贴元素的应用程序。 The problem is that if I disable selecting, copy events are also gone. 问题是,如果我禁用选择,复制事件也会消失。

I tried using 我试过用

onselectstart="return false;"

and

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

and it works, but it also disable copy event. 它工作,但它也禁用复制事件。

I also tried adding .no-select attribute only for these parts which contain text, but it is hard to maintain and does not work well - sometimes copy events are blocked and I cannot control it. 我也尝试仅为包含文本的这些部分添加.no-select属性,但它很难维护并且不能正常工作 - 有时复制事件被阻止而我无法控制它。

How can I disable select, but enable copy/paste proper way? 如何禁用选择,但启用正确的复制/粘贴方式?


Edit: 编辑:

  • I do not want to copy text, but my own json structures. 我不想复制文本,而是我自己的json结构。 Copying is handled in onCopy handler. 复制在onCopy处理程序中处理。
  • I need to subscribe to standard chrome copy events launched by chrome menu or system shortuts. 我需要订阅由chrome菜单或系统shortuts启动的标准chrome复制事件。

When you disabled highlighting/selecting then what you want copy? 当您禁用突出显示/选择时,您想复制什么? Not selected things is still nothing 未选择的东西仍然没有

I don't want to copy text (which is standard behaviour), but my own json representation of objects 我不想复制文本(这是标准行为),而是我自己的json表示对象

Then i have 2 solutions to your problem: 然后我有两个解决你的问题的方法:

  1. Override context menu with function copying to clipboard ( tutorial and library ) 使用函数复制到剪贴板覆盖上下文菜单( 教程

     if (document.addEventListener) { document.addEventListener('contextmenu', function(e) { alert("Write own menu with copy"); e.preventDefault(); }, false); } else { document.attachEvent('oncontextmenu', function() { alert("Write own menu with copy"); window.event.returnValue = false; }); } 
     body { -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } 
     <body> Some text </body> 

  2. Add "Copy" button with function copying to clipboard ( tutorial and library ) 添加“复制”按钮,功能复制到剪贴板( 教程

  3. Bind key combination ctrl + c (and other like command + c ) with function copying to clipboard ( tutorial and library ) 将键组合ctrl + c (以及其他类似command + c )与函数复制绑定到剪贴板( 教程
  4. Use Flash or other external browser addon to provide copy to clipboard function ( not recommended ) 使用Flash或其他外部浏览器插件提供复制到剪贴板功能( 不推荐

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM