复制时如何排除部分文本
我试图使一些文本不可复制,我的目的不是阻止人们从我的网站复制文本,而是使其更易于使用。我有一个包含文件大小的文件列表,但我只想复制文件名而不是文件大小。
到目前为止,我尝试了几种不同的方法,但没有奏效,我已经设法通过 CSS 用户选择使文本不可选择,但当我选择两行或更多行时,我仍然可以复制文件大小。
我只是尝试使用按钮来防止复制,但这不起作用,有什么想法吗?
<a href="http://10.10.10.1/links/file.doc"file.doc</a>
<button type="button" disabled="disabled" id="filesize">6 MB</button><br \>
目标浏览器是 Mac 上的 Safari,因此在最新 Safari 上运行的实验性 CSS3 或 HTML5 命令就可以了。
(感谢 mvds 提供建议的标题和一种解决方案)
Im trying to make some text non-copyable, my aim isn't to stop people from copying text from my website but more to make it easier to use. I have a list of files with file size's but I want to only copy the file names and not the file size.
So far i'v tried a few different methods but non have worked, I have managed to get the text non-selectable with CSS user-select but I can still copy the file size when I select two or more rows.
I just tried using a button to prevent the copying, which didn't work ether, any ideas?
<a href="http://10.10.10.1/links/file.doc"file.doc</a>
<button type="button" disabled="disabled" id="filesize">6 MB</button><br \>
Target browser is Safari on the Mac, so experimental CSS3 or HTML5 commands that work on the latest Safari is fine.
(Thanks mvds for the suggested title, and one solution)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
完成这项工作的一个相当老套的方法是使用
::before
或::after
伪元素来使用 csscontent:
属性插入文本。HTML:
CSS:
优点: 它有效:即使选择多行,它也不会复制到剪贴板。
缺点:伪元素需要特别注意 CSS 样式,因为它们是插入到正常文档流之外的。对多个元素使用
content:
属性还需要自定义标签或内联 CSS。A fairly hacky method to make this work is using
::before
or::after
pseudo elements to insert your text using csscontent:
property.HTML:
CSS:
Pros: It works: it doesn't get copied to the clipboard even when selecting multiple rows.
Cons: Pseudo elements require special care for CSS styling since they are inserted outside the normal document flow. Using the
content:
property for multiple elements would also require custom tags or inline css.丑陋的黑客警报:创建 2 个版本的文本,一种有尺寸,一种没有尺寸,其他方面相同,并将它们放在重叠的 div 中,没有尺寸的版本具有更高的 z-index。
更好的解决方案,也许不是跨浏览器,是添加一个
我建议重命名问题“复制时如何排除文本部分”,因为就目前情况而言,听起来您正在寻找平均无望的复制保护方案,虽然这个问题很有趣。
Ugly hack alert: create 2 versions of your text, one with and one without the sizes, otherwise identical, and put them in overlapping divs, with the version without sizes having a higher z-index.
The better solution, maybe not cross-browser, is to add an
I suggest renaming the question "how to exclude portions of text when copying" because as it stands now, it sounds like you're looking for the average hopeless copy-protection scheme, while the question is quite interesting.
使用跨度(或任何文本容器)和
unselectable
属性,如下所示:这并不适用于所有情况,但由于您有特定的浏览器目标,因此这将非常适合您的情况: )
为了轻松支持更多浏览器,您可以在其中添加一些 CSS,首先定义一个类,如下所示:
然后也分配它,如下所示:
Use a span (or any text container really) and the
unselectable
attribute, like this:This won't work everything, but since you have a specific browser target, this will work just fine for your situation :)
To easily support a few more browsers while you're at it, you can throw some CSS in there, first define a class like this:
Then assign it as well, like this: