首页 > 解决方案 > 如何禁止 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的功能不想工作。我该怎么办?对我的英语感到抱歉,并提前感谢您花时间阅读此主题!

标签: javascriptcssgoogle-apps-scriptcopytextselection

解决方案


推荐阅读