在文本框中设置的图像就像js中的占位符一样?

时间:2012-10-22 05:39:27

标签: javascript html5

我希望能够在填写文本框时隐藏某个图像,并且当文本框被清除时我想要显示某个图像。我知道它应该像这样工作,但用图像代替占位符:

$('input:text').focus(function () {
    if ($(this).val() === $(this).attr('placeholder')) {
        $(this).val('');
    }
}).blur(function() {
    if ($(this).val() == "") {
        $(this).val($(this).attr('placeholder'))
    }
});

提前感谢您的回答。

1 个答案:

答案 0 :(得分:0)

尝试 -

$('input:text').focus(function () {
    $(this).css('background', 'none');

}).blur(function() {
    if ($(this).val() == "") {
        $(this).css('background', 'url(http://profile.ak.fbcdn.net/hprofile-ak-prn1/71158_111108655596607_6920099_n.jpg) no-repeat left top');
    }
});

Check Demo

使用退格键 -

Updated Demo

相关问题