开发者

Input with Watermark (css & Jquery)

JS

$(document).ready(function () {
    $(":input[data-watermark]").each(function () {
        $(this).val($(this).attr("data-watermark"));
        $(this).bind('focus', function () {
            if ($(this).val() == $(this).attr("data-watermark")) $(this).val('');
        });
        $(this).bind('blur', function () {
            if ($(this).va开发者_StackOverflow中文版l() == '') $(this).val($(this).attr("data-watermark"));
            $(this).css('color','#a8a8a8');
        });
    });
});

HTML

<label>Name: </label>
<input class="input" type="text" name="name" maxlength="" data-watermark="My Name" />

CSS

.input{
    width:190px;
    height:16px;
    padding-top:2px;
    padding-left:6px;
    color:#000;
    font-size: 0.688em;
    border:#8c9cad 1px solid;
}

What I would like to fix is that whenever the watermarks is in value of the input that the color of the text should be grey (#a8a8a8). And when the value is something the user writes, then the color should be black.

This is the fiddle: http://jsfiddle.net/qGvAf/


Your desired behaviour of "Input with Watermark" has already been done - it's called the placeholder attribute. It works in modern browsers.

For older browsers, you should use a jQuery plugin to emulate placeholder for you.

Lastly, to set the colour, you need CSS similar to this:

.placeholder {
    color: #a8a8a8;
}
::-webkit-input-placeholder {
    color: #a8a8a8;
}
:-moz-placeholder {
    color: #a8a8a8;
}

Unfortunately, you can't combine the above rules; it won't work.


$(document).ready(function () {
    $(":input[data-watermark]").each(function () {
        $(this).val($(this).attr("data-watermark"));
        $(this).css("color","#a8a8a8");
        $(this).bind("focus", function () {
            if ($(this).val() == $(this).attr("data-watermark")) $(this).val('');
            $(this).css("color","#000000");
        });
        $(this).bind("blur", function () {
            if ($(this).val() == '') 
            {
                $(this).val($(this).attr("data-watermark"));
                $(this).css("color","#a8a8a8");
            }
            else
            {
                $(this).css("color","#000000");
            }
        });
    });
});


Don't override existing value's

I'll had a small problem with this nice solution, but with a little change solved, like to share it.

Just add the if statement on line 3 to prevent override loaded value's.

$(document).ready(function () {
    $('.watermark').each(function () {
    // add the line below
        if (this.value == '') {
            $(this).val($(this).attr("data-watermark"));
            $(this).css("color", "#a8a8a8");
            $(this).bind("focus", function () {
                if ($(this).val() == $(this).attr("data-watermark"))         
                $(this).val('');
                $(this).css("color", "#000000");
            });
            $(this).bind("blur", function () {
                if ($(this).val() == '') {
                    $(this).val($(this).attr("data-watermark"));
                    $(this).css("color", "#a8a8a8");
                }
                else {
                    $(this).css("color", "#000000");
                }
            });
    // Don't forget the closing bracket
        }
    })
});

Hope it's useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜