密码字段的水印文本

发布于 2024-11-19 10:13:01 字数 90 浏览 4 评论 0原文

我的页面上有一个密码字段。我想在输入密码之前在屏幕上的密码字段中显示水印文本“输入密码”,它将文本显示为密码类型。我们如何使用 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 技术交流群。

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

发布评论

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

评论(5

破晓 2024-11-26 10:13:01

最简单的方法是使用允许对密码字段添加水印的 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:

  • When your visible password input is empty, change it's type to text
    and display the watermark but leave the hidden input empty.
  • If a user gives it focus, hide the watermark and switch back to
    password.
  • As they type, copy the value into the hidden field.
  • If they leave the input and there's no text, switch the type back to
    text and show the watermark again.

Try either the plugin or coding the javascript yourself and let me know how it goes.

热风软妹 2024-11-26 10:13:01

所以下面的代码是一个用于常规输入字段的简单 jQuery 掩码。但这不适用于密码字段,因为字母被屏蔽了!这让我想到了两种可以扩展的方法。一种方法是自己编写一个屏蔽函数。因此,请使用常规输入字段,但在输入时屏蔽字符。另一种方法是在密码字段上覆盖一个常规文本框,然后隐藏它或在单击时更改 z 顺序。我知道您刚刚在评论中说您没有使用 jQuery,但我将其保留下来其他的。抱歉,我没有适合您的用例的代码!

/* Newsletter Sign Up Functions*/
    /* Toggle field descriptions as they are clicked in and out of */
    //set default input values because Firefox is stupid and doesn't reset them
    $("#newsletterform input[name=first]").val('First Name');
    $("#newsletterform input[name=last]").val('Last Name');
    $("#newsletterform input[name=email]").val('E-mail Address');

    //store default input values
    $("#newsletterForm input:text").each(function() {
        $.data(this, "initialval", $(this).val());
    });
    //clear values on focus
    $("#newsletterForm input:text").focus(function() {
        if ($.data(this, "initialval") == $(this).val()) { $(this).val(""); }
    });
    //restore value on lost focus
    $("#newsletterForm input:text").blur(function() {
        if ($(this).val() == "") { $(this).val($.data(this, "initialval")); }
    });
    /* End toggle field descriptions */

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!

/* Newsletter Sign Up Functions*/
    /* Toggle field descriptions as they are clicked in and out of */
    //set default input values because Firefox is stupid and doesn't reset them
    $("#newsletterform input[name=first]").val('First Name');
    $("#newsletterform input[name=last]").val('Last Name');
    $("#newsletterform input[name=email]").val('E-mail Address');

    //store default input values
    $("#newsletterForm input:text").each(function() {
        $.data(this, "initialval", $(this).val());
    });
    //clear values on focus
    $("#newsletterForm input:text").focus(function() {
        if ($.data(this, "initialval") == $(this).val()) { $(this).val(""); }
    });
    //restore value on lost focus
    $("#newsletterForm input:text").blur(function() {
        if ($(this).val() == "") { $(this).val($.data(this, "initialval")); }
    });
    /* End toggle field descriptions */
暖树树初阳… 2024-11-26 10:13:01

如果您只需要支持较新的浏览器,您可以在输入上使用 placeholder 属性。

<input type="password" placeholder="Insert Password" />

或者,如果您需要在脚本中设置它,请

input.placeholder =​​​​​​​ "Insert Password";

参阅http://jsfiddle.net/xECqY/

If you only need to support newer browsers you can use the placeholder attribute on the input.

<input type="password" placeholder="Insert Password" />

Or if you need to set it in script just do

input.placeholder =​​​​​​​ "Insert Password";

See http://jsfiddle.net/xECqY/.

清风疏影 2024-11-26 10:13:01

您绝对应该尝试 jQuery 水印功能,例如。 http://code.google.com/p/jwatermark/

You should definitely try a jQuery watermark function, ex. http://code.google.com/p/jwatermark/

八巷 2024-11-26 10:13:01

此 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/

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