简体   繁体   English

强制文本插入符号出现在只读输入中

[英]Force Text Caret To Appear In Readonly Input

Scenario: When an input field is set .readOnly = true , the text cursor is replaced with the pointer arrow cursor and the input field cannot be entered or modified.场景:当输入字段设置为.readOnly = true ,文本光标被指针箭头光标替换,输入字段无法输入或修改。 However, clicking into such a readonly input field with text in it does actually register the cursor's location within the text, even though the display does not show the clicked location by visually rendering a text cursor caret, like a read-enabled input field would.但是,单击这样一个带有文本的只读输入字段实际上会在文本中注册光标的位置,即使显示器不会像启用读取的输入字段那样通过视觉呈现文本光标插入符号来显示单击的位置。

Here's the question: Is there a way to force the text cursor caret to appear in an input field set .readOnly = true , in other words, as if the input field were actually read enabled, but still keep the input field readonly?这里的问题是:有没有办法强制文本光标插入符号出现在输入字段集.readOnly = true ,换句话说,就好像输入字段实际上已启用读取,但仍保持输入字段只读?

What you want to achieve is actually an existing bug in few browsers.您想要实现的实际上是少数浏览器中存在的错误。

Check this answer检查这个答案

But you can achieve this by below workaround.但是您可以通过以下解决方法来实现这一点。

Remove readOnly attribute and try with below code删除readOnly属性并尝试使用以下代码

 $('document').ready(function() { $('input[type="text"]').keydown(function(e) { return false; }); $('input[type="text"]').bind("cut copy paste", function(e) { e.preventDefault(); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" value="some value" />

You can do this with css :你可以用 css 做到这一点:

input[readonly] {
  cursor: text;
}

or或者

<input type="text" style="cursor: text;">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM