【问题标题】:Make text unselectable and uncopyable (webkit, while surrounded by copyable text)使文本不可选择和不可复制(webkit,同时被可复制文本包围)
【发布时间】:2016-08-29 21:10:40
【问题描述】:

下面的 sn-p 显示了如何使文本不可选择。遗憾的是,如果您在 Chrome 中选择任一侧的文本,当您复制和粘贴时,未选择的文本也会被粘贴。

有没有什么方法可以让你可以复制和粘贴大量文字,整个过程中包含不可选择的内容,并且没有任何不可选择的内容被粘贴?

.hide {
  color: orange;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
<div>Hello this text is selectable <span class="hide">but I'm not</span> You can select me all day!</div>

http://codepen.io/regan-ryan/pen/XdodGx

编辑: 这个问题看起来确实可能重复,看到关于这个主题已经有 12 个问题。但是,经过大量搜索后,我找不到我的特定问题,所以我认为它值得拥有一个更具体的问题标题。

【问题讨论】:

标签: html css selection


【解决方案1】:

更多解决方法:您可以利用 CSS 生成的内容对剪贴板 (*) 不可见这一事实,因此,将文本移动到某个属性的空白跨度与请求的剪贴板行为具有视觉上相似的结果:

[data-text] {
  color: orange;
}
[data-text]::after {
  content: attr(data-text);
}
<div>Hello this text is selectable <span data-text="but I'm not"></span> You can select me all day!</div>

http://codepen.io/myf/pen/jqXrNZ

如果可访问性/SEO 不是问题,这可能是解决方案。在其他情况下,它可能是 HTML 中的真实文本,但通过脚本“按需”移动到属性。

(*) 更新:如评论中所述 ( How to disable text selection highlighting using CSS? ) Internet Explorer 实际上涉及剪贴板中的 CSS 生成的内容。啊。

【讨论】:

  • 这很好用!我的内容是动态生成的,所以我最终得到了一个正则表达式解析 3 个组,之前 - (无副本) - 之后。然后我跑了这条JS线line = line.replace(spacerTextRegex, "$1<span class='spacer-text' data-uncopyable-text='$2'></span>$3");
  • 使用 CSS 放置文本是一种糟糕的可访问性做法。屏幕阅读器无法阅读此文本。
  • @SylvainDNS 可访问性是一个很好的观点,但据我所知,至少最近的屏幕阅读器可以很好地解析和阅读 CSS 生成的内容,考虑到它可以用作辅助(参见例如: adrianroselli.com/2017/12/tweaking-text-level-styles.html).
【解决方案2】:

css 可以禁用选择突出显示,但如果您不想使用不复制文本,请使用这段 jquery 代码

$('.hide').on('copy paste cut drag drop', function (e) {
   e.preventDefault();
});

$('.hide').on('copy paste cut drag drop', function (e) {
       e.preventDefault();
    });
.hide {
  color: orange;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Hello this text is selectable <span class="hide">but I'm not</span> You can select me all day!</div>

【讨论】:

  • 这在 Firefox 中运行良好,但在 Chrome 中似乎无法运行。
【解决方案3】:

您可以尝试使用window.getSelection 阅读突出显示的文本。请尝试以下代码示例并查看控制台。有了这个,您可以从字符串中删除不需要的文本并将其复制到剪贴板。但这不是很简单,甚至不可能。这只是一个想法。

function getSelectedText() {
  console.log(window.getSelection());
}

document.onmouseup = getSelectedText;
document.onkeyup = getSelectedText;
.hide {
  color: orange;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}
<!-- begin snippet: js hide: false -->
&lt;div&gt;Hello this text is selectable &lt;span class="hide" unselectable="on"&gt;but I'm not&lt;/span&gt; You can select me all day!&lt;/div&gt;

【讨论】:

  • 看起来无法选择,但我仍然可以用 Chrome 复制粘贴
【解决方案4】:

在样式属性或css文件中使用user-select: none;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 2019-07-28
    • 2020-10-03
    • 2012-09-07
    相关资源
    最近更新 更多