1

HTML 形式のテキスト フィールドにはデフォルト値がありますが、デフォルト値の代わりにプレースホルダーを表示したいと考えています。何か案は?

4

4 に答える 4

4

ここであなたが言ったことから、実際にはfocusイベントとblur<input>イベントをリッスンし、何も入力されていない場合は何らかのキャッシュを使用してコンテンツをクリアして復元したいようです。

<input id="foo" type="text" value="" data-value="" />

次にスクリプトで

var foo = document.getElementById('foo');
foo.addEventListener('focus', function () {
    this.setAttribute('data-value', this.value);
    this.value = '';
});
foo.addEventListener('blur', function () {
    if (this.value === '')
        this.value = this.getAttribute('data-value');
});

デモ

于 2013-07-23T02:48:01.413 に答える
2

HTML5 をサポートするブラウザーのみに関心がある場合は、次のオプションがあります。

<input type="text" name="myText" placeholder="My Placeholder">
于 2013-07-23T02:00:16.963 に答える
1

一方で、それは実行可能です。一方、なぜそうすべきなのかわかりません。

$('input[type="text"]').each(function (i, o) {
    var inputBox = $(o),
        swapInValue = function () {
            inputBox.val(inputBox.data('val'));
        },
        swapOutValue = function () {
            inputBox.data('val', inputBox.val()).val('');
        };
    inputBox.blur(swapOutValue).focus(swapInValue);
});
于 2013-07-23T02:06:43.263 に答える