隐藏文本字段闪烁插入符号

发布于 2024-09-18 03:35:05 字数 88 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

赏烟花じ飞满天 2024-09-25 03:35:10
function noCursor(a){
  var a = document.getElementById(a),
      b = document.createElement('input');
  b.setAttribute("style","position: absolute; right: 101%;");
  a.parentNode.insertBefore(b, a);

  if(a.addEventListener){
    b.addEventListener("input",function(){a.value = b.value});
    a.addEventListener("focus",function(){b.focus()});
  }else{
    a.attachEvent("onfocus",function(){b.focus()});
    b.attachEvent("onpropertychange",function(){a.value = b.value});
  };
 
}

noCursor('inp');
<input id="inp">

您可以对您不想使用光标的每个元素使用该函数。

function noCursor(a){
  var a = document.getElementById(a),
      b = document.createElement('input');
  b.setAttribute("style","position: absolute; right: 101%;");
  a.parentNode.insertBefore(b, a);

  if(a.addEventListener){
    b.addEventListener("input",function(){a.value = b.value});
    a.addEventListener("focus",function(){b.focus()});
  }else{
    a.attachEvent("onfocus",function(){b.focus()});
    b.attachEvent("onpropertychange",function(){a.value = b.value});
  };
 
}

noCursor('inp');
<input id="inp">

You can use the function for each element jou want no cursor for.

与酒说心事 2024-09-25 03:35:10

刚刚使用 @sinfere 的想法为我的一个朋友做了一个概念证明:

demo: http:// /jsfiddle.net/jkrielaars/y64wjuhj/4/

输入的开头是偏移的,因此它落在容器之外(隐藏了溢出)
实际的字符(和闪烁的光标)永远不会进入视图。
假 div 放置在输入字段下方,因此点击假 div 会将焦点设置在不可见的输入上。

<div class="container">
    <div id="fake" class="fake">
        <span class='star empty'></span>
        <span class='star empty'></span>
        <span class='star empty'></span>
        <span class='star empty'></span>
    </div>
    <input type="text" id="password" class="invisible" maxlength="4">
</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.

<div class="container">
    <div id="fake" class="fake">
        <span class='star empty'></span>
        <span class='star empty'></span>
        <span class='star empty'></span>
        <span class='star empty'></span>
    </div>
    <input type="text" id="password" class="invisible" maxlength="4">
</div>
风启觞 2024-09-25 03:35:09

适用于 iOS8。

<input style="position: fixed; top: -1000px">

Works in iOS8.

沉睡月亮 2024-09-25 03:35:09

将输入设置为只读也会这样做,因为它会阻止焦点,但它可能不适用于许多仍然需要它的用例。

<input type="text" readonly />

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.

<input type="text" readonly />
幸福还没到 2024-09-25 03:35:09

您可以通过调用焦点事件上的模糊函数来“隐藏文本字段闪烁光标”

<input type="text" onfocus="this.blur()"/>

you can "Hide textfield blinking cursor" by calling blur function on focus event

<input type="text" onfocus="this.blur()"/>
小糖芽 2024-09-25 03:35:08

不幸的是,您无法使用 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.

青萝楚歌 2024-09-25 03:35:08

我认为这是一个完美的解决方案:
使输入足够宽,右对齐屏幕右,从而使光标和内容位于屏幕外部,同时仍可点击
完美解决方案

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
perfect solution

魔法唧唧 2024-09-25 03:35:07

基本思想是,光标的颜色与文本的颜色相同。因此,您要做的第一件事就是使文本透明,从而将光标移开。然后,您可以使用文本阴影使文本再次可见。

使用此链接可在 jsfiddle 中查看它。

input[type="text"]{
    color : transparent;
    text-shadow : 0 0 0 #000;
}
input[type="text"]:focus{
    outline : none;
}

更新:

已知在iOS 8IE 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.

input[type="text"]{
    color : transparent;
    text-shadow : 0 0 0 #000;
}
input[type="text"]:focus{
    outline : none;
}

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.

余生共白头 2024-09-25 03:35:07

插入符号颜色:透明!重要; 适用于较新的浏览器

caret-color: transparent !important; works in newer browsers

獨角戲 2024-09-25 03:35:07

试试这个:

$(document).ready(
  function() {
    $("textarea").addClass("-real-textarea");
    $(".textarea-wrapper").append("<textarea class=\"hidden\"></textarea>");
    $(".textarea-wrapper textarea.hidden").keyup(
      function() {
        $(".textarea-wrapper textarea.-real-textarea").val($(this).val());
      }
    );
    $(".textarea-wrapper textarea.-real-textarea").focus(
      function() {
        $(this).parent().find("textarea.hidden").focus();
      }
    );
  }
);
.textarea-wrapper {
  position: relative;
}

.textarea-wrapper textarea {
  background-color: white;
}

.textarea-wrapper,
.textarea-wrapper textarea {
  width: 500px;
  height: 500px;
}

.textarea-wrapper textarea.hidden {
  color: white;
  opacity: 0.00;
  filter: alpha(opacity=00);
  position: absolute;
  top: 0px;
  left: 0px;
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<div class="textarea-wrapper">
  <textarea></textarea>
</div>

这个想法是在真实区域之上创建第二个不可见的

但它似乎在 IE8 中不起作用:'( 即使不透明度调到 11,插入符号仍然可见。

但它在 Firefox 中起作用...?

Try this:

$(document).ready(
  function() {
    $("textarea").addClass("-real-textarea");
    $(".textarea-wrapper").append("<textarea class=\"hidden\"></textarea>");
    $(".textarea-wrapper textarea.hidden").keyup(
      function() {
        $(".textarea-wrapper textarea.-real-textarea").val($(this).val());
      }
    );
    $(".textarea-wrapper textarea.-real-textarea").focus(
      function() {
        $(this).parent().find("textarea.hidden").focus();
      }
    );
  }
);
.textarea-wrapper {
  position: relative;
}

.textarea-wrapper textarea {
  background-color: white;
}

.textarea-wrapper,
.textarea-wrapper textarea {
  width: 500px;
  height: 500px;
}

.textarea-wrapper textarea.hidden {
  color: white;
  opacity: 0.00;
  filter: alpha(opacity=00);
  position: absolute;
  top: 0px;
  left: 0px;
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<div class="textarea-wrapper">
  <textarea></textarea>
</div>

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... ?

听闻余生 2024-09-25 03:35:07

我一直在寻找一种方法来隐藏 iOS 设备上触发日历的日期输入时闪烁的光标,因为您可以看到光标在日历选择器顶部闪烁。

input:focus { text-indent: -9999em; }

所以在这种情况下,我的 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.

input:focus { text-indent: -9999em; }

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文