Change an HTML5 input's placeholder color with CSS

后端 未结 30 3240
猫巷女王i
猫巷女王i 2020-11-21 04:36

Chrome supports the placeholder attribute on input[type=text] elements (others probably do too).

But the following CSS doesn\'t do anything

30条回答
  •  猫巷女王i
    2020-11-21 04:53

    I don't remember where I've found this code snippet on the Internet (it wasn't written by me, don't remember where I've found it, nor who wrote it).

    $('[placeholder]').focus(function() {
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
                input.removeClass('placeholder');
            }
        }).blur(function() {
            var input = $(this);
            if (input.val() == '' || input.val() == input.attr('placeholder')) {
                input.addClass('placeholder');
                input.val(input.attr('placeholder'));
            }
        }).blur();
        $('[placeholder]').parents('form').submit(function() {
            $(this).find('[placeholder]').each(function() {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                }
            })
        });
    

    Just load this JavaScript code and then edit your placeholder with CSS by calling this rule:

    form .placeholder {
       color: #222;
       font-size: 25px;
       /* etc. */
    }
    

提交回复
热议问题