HTML:光标显示在只读输入文本中?
假设我们有一个只读的文本框,如下所示:
<input type="text" readonly />
在 IE 9 和 FF 4 中,当我单击此字段时,该字段中会出现一个(不闪烁的)光标。然而,在 Chrome 中,光标不显示。 (请自行查看 http://jsfiddle.net/hqBsW/。)
我想我明白为什么 IE/FF 选择显示光标——这样用户就知道他或她仍然可以选择字段中的值。
尽管如此,这显然让我们的用户感到困惑,我们希望将 IE/FF 更改为不显示光标,就像 Chrome 对 readonly
字段所做的那样。
有办法做到这一点吗?
Let's say we have a textbox that's readonly, like so:
<input type="text" readonly />
In IE 9 and FF 4, when I click on this field, a (non-blinking) cursor appears in the field. In Chrome, however, the cursor does not show. (See for yourself at http://jsfiddle.net/hqBsW/.)
I suppose I understand why IE/FF opt to show the cursor—so the user knows he or she can still select the value in the field.
Nonetheless, it's evidently confusing our users and we would like to change IE/FF to not show the cursor, as Chrome does for readonly
fields.
Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我的解决方案,来自 nikmd23 的 jQuery 代码片段,但使用模糊()函数,似乎效果更好
示例:http://jsfiddle.net/eAZa2/< /a>
不要使用“disabled”属性,因为当输入是表单的一部分时不会提交
My solution, from nikmd23's jQuery snippet but with the blur() function that seems to work better
Example here: http://jsfiddle.net/eAZa2/
Don't use the attribute "disabled" because the input would not be submitted when it is part of a form
听起来像一个错误!
Mozilla 中存在类似的错误 (396542),它表示光标 < em>应该在只读输入中闪烁 - 但这种行为感觉不对,闪烁的光标应该意味着“你可以在这里输入”。
您应该对该错误发表评论并/或提交新错误(通过 Mozilla 此处 和 Microsoft 此处)!
与此同时,使用
disabled
或使用 JavaScript 更改行为(如 @nikmd23 所建议)似乎是最好的解决方法。Sounds like a bug!
There is a similar bug (396542) open with Mozilla, saying that the cursor should blink in
readonly
inputs — but that behavior feels wrong, a blinking cursor is supposed to mean, “you can type here.”You should comment on that bug and/or file new ones (with Mozilla here and with Microsoft here)!
In the meantime, using
disabled
or changing the behavior with JavaScript, as @nikmd23 suggested, seems like the best workaround.如果将
readonly
属性更改为disabled
,您将无法单击输入框,因此不会有光标。根据浏览器的不同,您可能也无法选择文本。
我在这里提供了各种输入状态的示例: http://jsfiddle.net/hqBsW/1/
另一种选择是当用户关注给定的输入元素时,您可以强制进行文本选择。控制行为的这种变化将更容易地向用户提示输入受到限制的事实,并且如果这是最终用例,则允许他们非常轻松地进行复制。
使用 jQuery,您可以编写如下选择代码:
我已更新示例以显示此行为的实际效果: http://jsfiddle.net /hqBsW/2/
If you change the
readonly
attribute todisabled
, you won't be able to click into the input box and thus won't have a cursor.Depending on the browser, you may not be able to select the text either though.
I've provided examples of the various input states here: http://jsfiddle.net/hqBsW/1/
Another alternative is you could force a text selection when the user focuses on a given input element. This change in control behavior would more easily clue the user into the fact that input is restricted, and allows them to copy very easily if that is the end use case.
Using jQuery you would write the selection code like this:
I've updated the example to show this behavior in action: http://jsfiddle.net/hqBsW/2/
对于仅
CSS
修复,请使用:For a
CSS
only fix, use :可以使用 html 和 javascript 完成
,也可以使用 jQuery 全局完成
It can be done using html and javascript
or globally using jQuery
您可以使用
css
:参考: https://developer.mozilla.org/pt-BR/docs/Web/CSS/pointer-events
You can use
css
:Reference: https://developer.mozilla.org/pt-BR/docs/Web/CSS/pointer-events
我找到的唯一方法是
the only way i found for this was
您可以设置标记的 style 属性,也可以通过设置 class 属性的值将 css 类添加到标记中。您的问题可以通过设置光标来解决。您可以在此处了解更多信息。另外,如果您有一些设置此标签光标的规则,您可以将 !important 添加到您的 css 规则中。您可以在此处了解有关 !important 的更多信息。
You can set the style attribute of your tag or you can add a css class to your tag by setting the class attribute's value. Your problem can be solved by setting the cursor. You can read more here. Also, if you have some rules which set the cursor of this tag you can add !important to your css rule. You can read more about !important here.