密码字段的水印文本
我的页面上有一个密码字段。我想在输入密码之前在屏幕上的密码字段中显示水印文本“输入密码”,它将文本显示为密码类型。我们如何使用 javascript 来解决这个问题?
I have one password field on page. I want to display watermark text "Enter password" in password field on screen before entering password it shows the text as password type. How we can resolve this issue by using javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最简单的方法是使用允许对密码字段添加水印的 jQuery 插件。我通过快速谷歌搜索想出了这个插件。
如果您不想使用 jQuery 插件,则需要 2 个 html 输入,一个可见,一个隐藏。隐藏的值包含您想要传回服务器的实际值,而可见的值是您使用 javascript 操作并与用户交互的值。下一部分是一起操作 2 个输入。以下是我能想到的重要部分:
并显示水印,但将隐藏的输入留空。
密码。
文本并再次显示水印。
尝试使用插件或自己编写 JavaScript,然后让我知道效果如何。
The easiest way is going to be to use a jQuery plugin that allows watermarking of password fields. I came up with this plugin doing a quick google search.
If you don't want to use a jQuery plugin, what you're going to need is 2 html inputs, one visible and one hidden. The hidden one contains the actual value you're wanting to pass back to the server while the visible one is the one you manipulate with javascript and the user interacts with. The next part is to manipulate the 2 inputs together. Here's the important parts that I can think of to code for:
and display the watermark but leave the hidden input empty.
password.
text and show the watermark again.
Try either the plugin or coding the javascript yourself and let me know how it goes.
所以下面的代码是一个用于常规输入字段的简单 jQuery 掩码。但这不适用于密码字段,因为字母被屏蔽了!这让我想到了两种可以扩展的方法。一种方法是自己编写一个屏蔽函数。因此,请使用常规输入字段,但在输入时屏蔽字符。另一种方法是在密码字段上覆盖一个常规文本框,然后隐藏它或在单击时更改 z 顺序。我知道您刚刚在评论中说您没有使用 jQuery,但我将其保留下来其他的。抱歉,我没有适合您的用例的代码!
So the below code is a simple jQuery mask for a regular input field. But this won't work for a password field because the letters are masked! So that makes me think of two ways this could be extended. One would be to write a masking function yourself. So use a regular input field, but mask characters as they are entered. The other would be to overlay a regular textbox on the password field and then either hide it or change the z-order when clicked in. I know that you just said in comments you aren't using jQuery, but I am leaving this up for others. Sorry I don't have code for your use case!
如果您只需要支持较新的浏览器,您可以在输入上使用
placeholder
属性。或者,如果您需要在脚本中设置它,请
参阅http://jsfiddle.net/xECqY/。
If you only need to support newer browsers you can use the
placeholder
attribute on the input.Or if you need to set it in script just do
See http://jsfiddle.net/xECqY/.
您绝对应该尝试 jQuery 水印功能,例如。 http://code.google.com/p/jwatermark/
You should definitely try a jQuery watermark function, ex. http://code.google.com/p/jwatermark/
此 jQuery 插件得到积极维护并处理新的 HTML5 输入字段:http://code.google.com /p/jquery-watermark/
This jQuery plugin is actively maintained and handles new HTML5 input fields: http://code.google.com/p/jquery-watermark/