【问题标题】:issue with disabling text selection of textarea [duplicate]禁用textarea的文本选择问题[重复]
【发布时间】:2021-06-12 00:35:30
【问题描述】:

stackoverflow 上有很多无效的答案,但我正在寻找一个有效的答案:

const textarea = document.getElementsByTagName("textarea")[0];
textarea.style.userSelect = 'none';
textarea.readOnly = true;
textarea.style.cursor = 'default';
textarea.setAttribute("unselectable", "on");
<textarea>
At w3schools.com you will learn how to make a website. They offer free tutorials in all web development technologies.
</textarea>

我想使用纯 javascript(仅)在 chrome 中禁用此 textarea 上的文本选择。

但由于某些原因(可能是错误),这不起作用!

【问题讨论】:

标签: javascript html css


【解决方案1】:

不太确定这是否是您要查找的内容。我想您不希望在 chrome 上选择文本,它应该可以在其他浏览器上正常工作

var isChrome = !!window.chrome;

if (isChrome) {
  const textarea = document.getElementsByTagName("textarea")[0];
  textarea.style.userSelect = 'none';
  textarea.readOnly = true;
  textarea.style.cursor = 'default';
  textarea.setAttribute("unselectable", "on");
  textarea.setAttribute("onselectstart", "return false;");
  textarea.setAttribute("onmousedown", "return false;");
}
<textarea>
Some content
</textarea>

【讨论】:

  • 像 chrome 上的魅力一样工作......
  • @Sara Ree。很高兴这有效!
猜你喜欢
  • 2020-08-23
  • 2014-03-21
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 2016-11-28
相关资源
最近更新 更多