使用此javascript,占位符适用于IE / Firefox中的文本但不适用于密码框 [英] placeholder works for text but not password box in IE/Firefox using this javascript

查看:73
本文介绍了使用此javascript,占位符适用于IE / Firefox中的文本但不适用于密码框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Js档案

//Modified from http://www.beyondstandards.com/archives/input-placeholders/
function activatePlaceholders()
{
    var detect = navigator.userAgent.toLowerCase(); 
    if (detect.indexOf("safari") > 0) return false;
    var inputs = document.getElementsByTagName("input");
    for (var i=0;i<inputs.length;i++)
    {
        if (inputs[i].getAttribute("type") == "text")
        {
            var placeholder = inputs[i].getAttribute("placeholder");
            if (placeholder.length > 0)
            {
                inputs[i].value = placeholder;
                inputs[i].onclick = function()
                {
                    if (this.value == this.getAttribute("placeholder"))
                    {
                        this.value = "";
                    }
                    return false;
                }
                inputs[i].onblur = function()
                {
                    if (this.value.length < 1)
                    {
                        this.value = this.getAttribute("placeholder");
                    }
                }
            }
        }
        else if (inputs[i].getAttribute("type") == "password")
        {
            var placeholder = inputs[i].getAttribute("placeholder");
            if (placeholder.length > 0)
            {
                inputs[i].value = placeholder;
                inputs[i].onclick = function()
                {
                    if (this.value == this.getAttribute("placeholder"))
                    {
                        this.value = "";
                    }
                    return false;
                }
                inputs[i].onblur = function()
                {
                    if (this.value.length < 1)
                    {
                        this.value = this.getAttribute("placeholder");
                    }
                }
            }
        }
    }
}
window.onload = function()
{
    activatePlaceholders();
}

Html part

Html part

<form name="input" action="login.php" method="post">
        <table width="*" border="0">
        <tr>
        <td align="left"><input type="text" name="username" placeholder="Username" />&nbsp;<input type="password" name="pass" placeholder="Password" /><input type="submit" value="Login" /></td>
        </tr>
        <tr>
        <td colspan="2" align="right">Stay logged in:&nbsp;<input type="checkbox" name="remember" value="1">&nbsp;&nbsp;Sign Up | Forgot Password&nbsp;&nbsp;&nbsp;</td>
        </tr>
        </table>
        </form>

这适用于输入框。不适用于密码框。它希望在密码上标出占位符文本。我希望用户密码可以启动,但不是占位符。不确定如何解决这个问题,以便在IE和Firefox中运行。

This works for the input box. Not working for the password box. It wants to star out the place holder text on the password. I want to users password to be started out but not the place holder. Not sure how to fix that so it works in IE and Firefox.

推荐答案

var passwords = document.getElementsByTagName("password");

应该是:

var passwords = document.getElementsByTagName("input");

因为它是< input type =password> 不是<密码...> 。但是,在这种情况下,我认为这不会对你有帮助,因为当你设置密码的值时,所有你会看到的是黑点/星号。

As it's <input type="password"> not <password...>. However, I don't think that will help you in this case, because when you set the value of the password, all you'll see are the black dots/asterisks.

这篇关于使用此javascript,占位符适用于IE / Firefox中的文本但不适用于密码框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆