【问题标题】:Textarea not selectable in Chrome在 Chrome 中无法选择文本区域
【发布时间】:2018-07-12 19:20:39
【问题描述】:

是否可以在谷歌浏览器(我使用的是 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>

【问题讨论】:

  • 我正在使用与您的版本号相同的 Google Chrome,OSX 为 64 位,您的代码适用于我。我无法集中注意力,也无法在文本区域中书写。
  • 我认为您可以添加pointer-events: none;,它应该可以解决问题。
  • 你可以做一个鼠标按下事件,获取被点击元素的id,并防止默认行为
  • @newbie 从理论上讲,几乎所有浏览器都不需要 JavaScript 即可在 CSS 中运行。 Fabio_MO 你清空浏览器缓存了吗?
  • @AnthonyB 正如你所说,在几乎所有浏览器中,但他没有指定他正在寻找的解决方案,这就是我添加这个的原因,所以他可以选择:)

标签: javascript html css google-chrome


【解决方案1】:

试试下面的代码。用户将无法使用此 CSS 选择任何文本。而且我检查了许多浏览器,它们与所有浏览器兼容,因此您可以使用此 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>

【讨论】:

  • 不起作用....行为是一样的。仅 CSS 中的 ::selection “隐藏”选择颜色,但文本仍然可选择
  • 在 Chrome 63 中不适合我 - 我无法选择文本
【解决方案2】:

这应该也可以

.mytextarea::selection {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
<textarea class="mytextarea" oncontextmenu="return false" readonly=”readonly” 
style="background-color: #FFFFFF; width:400px; height:100px;color:grey">
    Test Test Test Test Test Test
        Test Test Test Test Test Test
        Test Test Test Test Test Test
        Test Test Test Test Test Test
        Test Test Test Test Test Test
        Test Test Test Test Test Test
        Test Test Test Test Test Test
        Test Test Test Test Test Test
        Test Test Test Test Test Test
        Test Test Test Test Test Test
        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>

【讨论】:

  • 你改变了什么?如果被否决,也不要删除并再次回答。
  • 这对我来说是最好的解决方案。谢谢:)
  • @mplungjan 我没有放 readonly="readonly" 所以我改变了解决方案......!
  • 在这里,用户可以选择测试。这是因为它似乎不是由用户简单选择的,而是当用户按下复制命令时文本将被复制。
【解决方案3】:

如果你愿意,你可以用一个包装器 div 来点缀它,而不是像这样的 jQuery:

document.querySelector("#toggle").addEventListener('click', () => {
	
  document.querySelector('.wrapper').classList.toggle("disabled");

});
.wrapper {
  height: 100px;
  width: 400px;
}

.disabled .mask {
  position: absolute;
  top: 0;
  left: 0;
  height: 100px;
  width: 390px;
  z-index: 10;
}

.disabled .mytextarea {
  position: absolute;
  top: 0;
  left: 0;
  margin-left: 1px;
  z-index: 0;
}
        
.mask {
   z-index:0;
}
  
.mytextarea {
    z-index: 10;
}
<div class="wrapper disabled">
  <div class="mask">

  </div>

  <textarea  id="objLog" class="mytextarea"  readonly style="background-color: #FFFFFF;height:92px;width: 394px; resize: none;">
    Test Test Test Test Test Test
    Test Test Test Test Test Test
    Test Test Test Test Test Test
    Test Test Test Test Test Test
    Test Test Test Test Test Test
    Test Test Test Test Test Test
    Test Test Test Test Test Test
    Test Test Test Test Test Test
    Test Test Test Test Test Test
    Test Test Test Test Test Test
    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>
</div>

<button id="toggle" style="margin-top: 40px;">
toggle
</button>

【讨论】:

  • 这几乎是我要搜索的内容,但使用此解决方案,当文本区域不可选择时无法滚动。
  • 你是对的。只需调整蒙版宽度。答案更新! (但现在用户可以更改文本区域的大小......)
  • 为了完成我之前的评论,我只是将 textarea 的 css resize 属性更改为 none。
猜你喜欢
  • 2012-02-22
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多