隐藏文本字段闪烁插入符号
我的 HTML 中有一个文本输入。当输入获得焦点时,有没有办法隐藏闪烁的文本插入符号?
我愿意使用 JavaScript 或 CSS 来实现这一点。
I have a text input in my HTML. Is there a way to hide the blinking text caret when the input gets focus?
I am open to doing this with JavaScript or CSS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以对您不想使用光标的每个元素使用该函数。
You can use the function for each element jou want no cursor for.
刚刚使用 @sinfere 的想法为我的一个朋友做了一个概念证明:
demo: http:// /jsfiddle.net/jkrielaars/y64wjuhj/4/
输入的开头是偏移的,因此它落在容器之外(隐藏了溢出)
实际的字符(和闪烁的光标)永远不会进入视图。
假 div 放置在输入字段下方,因此点击假 div 会将焦点设置在不可见的输入上。
just made a proof of concept for a friend of mine, using @sinfere 's idea:
demo: http://jsfiddle.net/jkrielaars/y64wjuhj/4/
The start of the input is offset so it falls outside of the container (which has overflow hidden)
The actual caracters (and blinking cursor) wil never enter into view.
The fake div is placed below the input field so a tap on the fake div will set focus on the invisible input.
适用于 iOS8。
<input style="position: fixed; top: -1000px">
Works in iOS8.
将输入设置为只读也会这样做,因为它会阻止焦点,但它可能不适用于许多仍然需要它的用例。
Setting the input to
readonly
also does this since it prevents focus but it may not be applicable to many use cases that still need it.您可以通过调用焦点事件上的模糊函数来“隐藏文本字段闪烁光标”
you can "Hide textfield blinking cursor" by calling blur function on focus event
不幸的是,您无法使用 CSS 设置文本光标的样式。你只能做一些非常糟糕的 JavaScript 技巧,但根据你网站的布局和要求,这可能根本不可能。所以我建议忘记整个想法。
Unfortunately you can not style the text cursor with CSS. You can only do some very bad JavaScript tricks but depending on the layout and requirements of your website, it might not be possible at all. So I would recommend to forget the whole idea.
我认为这是一个完美的解决方案:
使输入足够宽,右对齐屏幕右,从而使光标和内容位于屏幕外部,同时仍可点击
I think this is a perfect solution:
make the input wide enough, align right to screen right, thus make cursor and content locate at the outside of the screen, while it's still clickable
基本思想是,光标的颜色与文本的颜色相同。因此,您要做的第一件事就是使文本透明,从而将光标移开。然后,您可以使用文本阴影使文本再次可见。
使用此链接可在 jsfiddle 中查看它。
更新:
已知在iOS 8和IE 11中不起作用
我的另一个想法是有点更hacky并且需要javascript。
HTML 和 CSS 部分:
您创建 2 个输入字段,并使用 z-index 等将一个输入字段精确地放置在另一个输入字段的顶部。然后,您使顶部输入字段完全透明,没有焦点,没有颜色,并且一样。
您需要将可见的、较低的输入设置为禁用,这样它只显示上面输入的内容,但实际上不起作用。
Javascript部分:
完成上述操作后,您将同步两个输入。在按键或更改时,您将较高输入的内容复制到较低输入。
总结以上所有内容:您输入一个不可见的输入,该输入将在提交表单时发送到后端,但其中文本的每次更新都将回显到下方可见但禁用的输入字段中。
The basic idea is, that the cursor's color is the same as the text's color. So the first thing you do is make the text transparent, thus taking the cursor away with it. Then you can make the text visible again with a text shadow.
Use this link to see it live in jsfiddle.
Update:
Known to not work in iOS 8 and IE 11
Another idea of my is a bit more hacky and requires javascript.
HTML and CSS part:
You make 2 input fields and position one exactly on top of the another with z-index, etc. Then you make the top input field completely transparent, no focus, no color, and alike.
You need to set the visible, lower input to disabled, so that it only shows the content of the above input, but not actually works.
Javascript part:
After all the above you sync the two inputs. On keypress or on change you copy the contents of the higher input to the lower.
Summing all the above: you type in an invisible input, and that will be sent to the backend when the form submitted, but every update of the text in it will be echoed into the lower visible, but disabled input field.
插入符号颜色:透明!重要;
适用于较新的浏览器caret-color: transparent !important;
works in newer browsers试试这个:
这个想法是在真实区域之上创建第二个不可见的
但它似乎在 IE8 中不起作用:'( 即使不透明度调到 11,插入符号仍然可见。
但它在 Firefox 中起作用...?
Try this:
The idea is to create a second, invisible
<textarea>
over/on-top-of the real one. The user is typing in the invisible one but the text doesn't appear (nor the caret/cursor) as it is invisible! You then use JavaScript to assign its value to the visible one.But it doesn't seem to work in IE8 :'( the caret is still visible even though the opacity is cranked up to 11.
But it works in Firefox... ?
我一直在寻找一种方法来隐藏 iOS 设备上触发日历的日期输入时闪烁的光标,因为您可以看到光标在日历选择器顶部闪烁。
所以在这种情况下,我的 CSS 工作得很好,显然缺点是,如果你需要查看你正在输入的内容,那么它就不好
I was looking for a way to hide the blinking cursor on iOS devices for date inputs that trigger a calendar, because you could see the cursor blinking on top of the calendar picker.
So in this case my CSS works nicely, obviously the downside is that if you need to see what you are typing then it is not good