javascript - 如何禁止 USER 突出显示选择文本,但除非特别是 SCRIPT?
问题描述
我有脚本可以使某些事情相互冲突并使我也困惑于解决它。直截了当,请参阅下面的脚本:
======================================== CSS =========== ========================
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
outline-style: none;
user-select: none;
上面几行的含义是用户不允许选择文本。而javascript的功能可以帮助CopyText函数继续运行选择文本,然后将其复制到剪贴板,仅用于该函数的脚本以允许选择文本。请参阅下面的 Javascript:
========================================= JS ========== ==========================
function toClipboard(e) {
var cell = e.parentNode,
copyText = cell.getElementsByTagName('span'),
selection = window.getSelection(),
range = document.createRange();
range.selectNodeContents(copyText[0]);
selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
event.stopPropagation();
}
然后用于运行上述所有脚本的HTML适用于表格场景。见下文:
======================================== HTML ========== =======================
...
<tr OnClick=highlight(this)>
<td>test to copy this text <button onclick=toClipboard(this)><small>Copy</small></button></td>
...
注意: 现在的问题是toClipboard的功能不想工作。我该怎么办?对我的英语感到抱歉,并提前感谢您花时间阅读此主题!
解决方案
推荐阅读
- autofac - 有没有办法使用 autofac 在 asp.net 核心中注册特定于租户的控制器?
- reactjs - 如何使用带有材质 ui 样式 api 的伪选择器?
- java - ESAPI - 使用 ESAPI 2.2.3.1 获取 ClassNotFoundException
- r - 如何在 Shiny 中更改填充的背景颜色
- python - PyQtGraph 3D 功能
- css - 在 flex 行中保持同级元素的水平和垂直定位
- airflow-scheduler - Airflow 中的动态用户模拟
- java - 构建 Maven 项目失败
- android - 如何递归地将小部件添加到颤动布局
- blockchain - 是否有可能开发一个只购买 ERC-721 代币/NFTS 的 ERC-20 代币?好像erc20是erc721锁的钥匙?