JS/CSS/XHTML:在复制事件期间不要复制特定文本
我正在寻找一种方法,当您使用 Ctrl + C 等时禁用特定文本区域的复制。无论我是否必须以不同的方式编写文本。
http://gyazo.com/721a0a5b5af173beb1ad3305633beafb.png
以上是它的用途。这是我一直在开发的语法荧光笔(到目前为止支持 3 种语言)。当用户以任何方式选择任何文本时,我不希望复制行号。
我想不出一种方法来显示行号,而它们实际上并不存在。
I'm looking for a way to disable the copying of a specific area of text when you use Ctrl + C, etc. Whether I have to write the text a different way or not.
http://gyazo.com/721a0a5b5af173beb1ad3305633beafb.png
Above is what this is for. It's a syntax highlighter I have been working on (3 languages supported so far). When the user selects ANY text in any way, I don't want the line numbers to be copied.
I can't think of a way to display line numbers, without them actually being there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只要行号和源代码混合在一起,就很难以编程方式阻止(如果不是不可能的话)。
理想的方法是将源代码放在自己的实际容器中。
打开文档检查器并查看 Github 是如何做到的,例如: https:// github.com/jbrisbin/riak-exchange/blob/master/Makefile
他们有一个单独的
As long as the line numbers and the source code are mixed together, this is going to be tough to prevent programmatically, if not impossible.
The ideal way would be having the source code in an actual container of its own.
Open a document inspector and look at how Github do it, for example: https://github.com/jbrisbin/riak-exchange/blob/master/Makefile
they have a separate
<pre>
element containing the line numbers, and a<table>
cell containing the code. (I assume selecting is a reason why they use tables here, but I do not know for sure.)尝试一下...
演示:http://jsfiddle.net/wdm954/UD8Dq/7
我对 div 进行了分层,因此代码 div 位于顶部,数字位于后面。当您复制并粘贴时,您应该只获得代码。
Give this a try...
Demo: http://jsfiddle.net/wdm954/UD8Dq/7
I layered the div so the code div is on top and the numbers are behind. When you copy and paste you should just get the code.
将
user-select
、-moz-user-select
和-webkit-user-select
设置为none
可能会工作。对于 IE,您需要处理onselectstart
并返回 false。这将阻止人们选择该文本,但我不知道当它位于您尝试复制的其他文本旁边时会发生什么。
Setting
user-select
,-moz-user-select
, and-webkit-user-select
tonone
might work. For IE, you will need to handleonselectstart
and return false.This will prevent people from selecting the text, but I don't know what happens when it's beside other text that you attempt to copy.
我知道这个问题已经有三年了,但是使用 HTML5,您可以将行号存储在数据属性中,并使用 CSS2 来显示文本。这应该可以防止行号被复制。
HTML
CSS
I know that this question is three years old, but with HTML5 you can store line numbers in a data attributes and use CSS2 to display the text. This should prevent line numbers from being copied.
HTML
CSS