0

私のアプリケーションでは、テキスト フィールドとテキストエリア フィールドのプレースホルダーが必要です。Internet Explorer がプレースホルダーをサポートしていないことは知っています。私は周りを見回していて、テキストフィールドでのみうまく機能するフォールバックコードを見つけました。これをテキストエリアでも機能させるにはどうすればよいですか。

コード:

jQuery(function() {
    jQuery.support.placeholder = false;
    test = document.createElement('input');
    if('placeholder' in test) jQuery.support.placeholder = true;
});

$(function() {
    if(!$.support.placeholder) { 
        var active = document.activeElement;
        $('input[type=text], textarea').focus(function () {
            if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
                $(this).val('').removeClass('hasPlaceholder');
            }
        }).blur(function () {
            if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
                $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
            }
        });
        $('input[type="text"], textarea').blur();
        $(active).focus();
        $('form').submit(function () {
            $(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
        });
    }
});

HTML:

                <li>
                    <input type="text" placeholder="To:" id="to" autocapitalize="off" autocorrect="off" autocomplete="off" />
                </li>
                <li>
                    <textarea placeholder="Message:" id="body" rows="10" cols="30" autocapitalize="off" autocorrect="off" autocomplete="off"></textarea>
                </li>
4

4 に答える 4

6

$(':text')に変更$('input[type="text"], textarea')

または、この既存の jQuery プラグイン:

https://github.com/mathiasbynens/jquery-placeholder

テキストエリアとパスワードフィールドを含む多くのタイプの入力フィールドで動作します

于 2012-07-31T20:36:20.927 に答える
2
<script type="text/javascript">
 $(function() {
    if(!$.support.placeholder) { 
        var active = document.activeElement;
        $('input[type="text"], textarea').focus(function () {
            if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
                $(this).val('').removeClass('hasPlaceholder');
            }
        }).blur(function () {
            if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
                $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
            }
        });
        $('input[type="text"], textarea').blur();
        $(active).focus();
        $('form').submit(function () {
            $(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
        });
    }
});
</script>
于 2013-06-10T12:16:31.020 に答える
0

テキストセレクターを使用しています。テキストエリアも使用するように変更してください。

$(':text')

する必要があります

$(':text, textarea')

またはより良いパフォーマンスの使用のために

$('input[type=text], textarea')
于 2012-07-31T20:34:07.293 に答える
0

に置き換える必要があるよう:textです:text,textarea

于 2012-07-31T20:35:26.310 に答える