javascript 如何隐藏html文本框中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3324099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:51:35  来源:igfitidea点击:

how to hide text in an html textbox

javascripthtml

提问by Mac

i am having a simple html text box what i want just as the user type something in it the text should be invisible but the text should be there

我有一个简单的 html 文本框,我想要什么,就像用户在其中键入内容一样,文本应该不可见,但文本应该在那里

<html>
<head>
<title>My Page</title>
<script type="text/javascript">
function hide()
{
document.forms["text"].style.visibility = 'hidden';
}
</script>

</head>
<body>
<form name="myform">
<div align="center">
<input type="text" size="25" onkeyup="return hide();">
</div>
</form>
</body>
</html>

回答by kander

Mac,

苹果电脑,

What you're doing is kind-of impossible to accomplish using straightforward means. When you use the code you've included in the question, you're actually removing the visibility of the entire box, not just the text content.

您正在做的事情使用简单的方法几乎无法完成。当您使用问题中包含的代码时,您实际上是在删除整个框的可见性,而不仅仅是文本内容。

Some ideas:

一些想法:

  1. Make the textcolor the same as the background color.
  2. Use the onKeyPress event, after every keystroke take the value from this box, and append it into a hidden textbox you've got elsewhere on the page (or a javascript variable.
  1. 使文本颜色与背景颜色相同。
  2. 使用 onKeyPress 事件,在每次击键后从该框中获取值,并将其附加到页面上其他位置的隐藏文本框(或 javascript 变量)中。

Combining these should be pretty effective in what you're tryin to do.

将这些结合起来应该对您尝试做的事情非常有效。

More important is the question: Why would you want to do this? Could you elaborate on what it is you're trying to accomplish here?

更重要的是一个问题:你为什么要这样做?你能详细说明你在这里想要完成什么吗?

Edit: I see you're doing this for passwords. In that case, why not use the <input type="password"> field? That way the browser know it's a password field and hides the input automatically (using the standard dots, or stars).

编辑:我看到你这样做是为了密码。在这种情况下,为什么不使用 <input type="password"> 字段?这样浏览器就会知道这是一个密码字段并自动隐藏输入(使用标准点或星号)。

回答by nyn3x

instead of setting visibility="hidden"try display="none"

而不是设置visibility="hidden"尝试display="none"

Edit: didn't noticed it's about passwords - in this case you should use type=passwordfor the input element.

编辑:没有注意到它与密码有关 - 在这种情况下,您应该将其type=password用于输入元素。

回答by Brian S

It looks like you're trying to make a homebrew password field? It would be much simpler to just use <input type="password" ...>instead.

看起来您正在尝试制作自制密码字段?使用它会简单得多<input type="password" ...>

回答by Chase Wilson

For a Password protected field please just use the "password" type.

对于受密码保护的字段,请仅使用“密码”类型。

<input type="password" size="25" />