javascript - 阻止选择在 IE 中有效但在 Chrome 中无效的文本?

标签 javascript css angularjs google-chrome browser

我看过几篇关于阻止选择文本的不同帖子,但由于某种原因,此代码阻止在 IE 中进行选择,但在 Chrome 中则不然。我不明白为什么会这样。

 <input type="text" class="form-control no-select" name="inputField" placeholder="Select" ng-model="ctrl.ngModelValue">

 .no-select {
  -webkit-touch-callout: none; /* iOS Safari */
  -webkit-user-select: none;   /* Chrome/Safari/Opera */
  -khtml-user-select: none;    /* Konqueror */
  -moz-user-select: none;      /* Firefox */
  -ms-user-select: none;       /* IE/Edge */
  user-select: none;  
}  

我基本上想防止输入中文本(ng-model)的突出显示。

上面的代码阻止了我在 IE 中选择文本,但由于某种原因我仍然可以在 Chrome 中突出显示输入元素中的文本?当涉及到某些工作时,通常情况正好相反。

谁能告诉我为什么会这样以及如何解决它?

谢谢!

最佳答案

嗯,我不知道如何使用css解决这个问题在input场,user-select: none规则也适用于其他标签,例如 <p> <div>等等

这里是JavaScript为您提供解决方案

参见FIDDLE

var inp = document.getElementById('myElement');
inp.addEventListener('select', function() {
  this.selectionStart = this.selectionEnd;
}, false);
.no-select {
  -webkit-touch-callout: none; /* iOS Safari */
  -webkit-user-select: none;   /* Chrome/Safari/Opera */
  -khtml-user-select: none;    /* Konqueror */
  -moz-user-select: none;      /* Firefox */
  -ms-user-select: none;       /* IE/Edge */
  user-select: none;  
}  
 <input type="text" id="myElement" class="form-control no-select" name="inputField" placeholder="Select" ng-model="ctrl.ngModelValue">
 
 <p class="no-select">
 Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque quas, illo iste! Obcaecati quisquam, error doloremque, numquam iste, laboriosam possimus nostrum optio aperiam, distinctio quo accusamus cum molestias! Est, at!
 </p>

关于javascript - 阻止选择在 IE 中有效但在 Chrome 中无效的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36441915/

相关文章:

javascript - 正则表达式 - 从字符第 N 次出现到字符串末尾的匹配

html - Rems 在 Chrome 和 Firefox 之间呈现不同

css - Gulp 4 compilling scss 创建空的 css 文件,但 sass 编译正确

html - iframe 不显示滚动条

javascript - JQuery $.ajax() 函数没有准确运行 POST 请求

javascript - Bootstrap 导航栏链接不起作用

javascript - 使用 Angular JS 的自定义分页 - 需要在第一页显示 div 元素,该元素应该在第二页上重复单击下一步

javascript - 如何应用带过滤器的 ng-if 来过滤记录并在同一页面中呈现两个不同的 View ?

javascript - 使用 xpath 单击 aria-label 元素

css - 为什么不在div中将列表设置为斜体