javascript - 在 Chrome 中无法选择文本区域

标签 javascript html css google-chrome

在谷歌浏览器(我使用的是 63.0.3239.132 版本)中是否可以使文本区域不可选择(只有内部文本不可选择)?

此代码在 Edge、Explorer 和 Firefox 中有效,但在 Chrome 中无效。

还有其他样式属性要添加吗?

.mytextarea {
  cursor: default;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -webkit-user-select: none;
}
<textarea id="objLog" class="mytextarea" disabled="disabled" readonly style="background-color: #FFFFFF; width:400px; height:100px;">Test Test Test Test Test Test Test Test Test Test Test Test</textarea>

最佳答案

Try the code below. The user will not be able to select any text with this CSS. And I've checked with many browsers, they are compatible with all browsers so you can use this CSS.

因为最好有一个可以被其他用户使用的解决方案,而不仅仅是一个提问者。

$(document).ready(function() {
  $('#objLog').bind("select", function(e) {
    e.preventDefault();
    $(this).text($(this).text());
    return false;
  });
});
.mytextarea {
  cursor: default;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
  background: #fff;
  width: 400px;
  height: 100px;
  color:grey
}

.mytextarea::-moz-selection {
  color:grey;
  background: #fff;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.mytextarea::selection {
  color:grey;
  background: #fff;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class="mytextarea" id="objLog" 
  onselect="return false;"
   readonly="readonly"
   unselectable="on"
   disabled="disabled"
>Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test</textarea>

关于javascript - 在 Chrome 中无法选择文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48580163/

相关文章:

javascript - ie7 无法使用 javascript 循环遍历字符串

javascript - Firefox 未检测到 iframe 加载事件?

html - 图像在 CSS3 高度过渡时模糊

javascript - 如何在 ionic 中更改 main.css 的类

javascript - 使用 noUiSlider 通过 onclick 事件创建范围时出错

javascript - 样式化组件/React - 外部元素样式

javascript - Angular JS过滤器: Why does this remove other LIKE options?

html - SVG 使用元素引用外部文件在 Safari 中不起作用

HTML & CSS : Using CSS to properly align <h1> and <p> tags

jquery - 如何将 jquery UI slider 与 jqueryrotate 插件相结合?