4

data-* 属性を使用してプレースホルダーを設定したい。jQueryでやってみました。しかし、機能していません。すべての入力タイプの代わりに id を使用する場合。その働き。

しかし、ページ内の入力テキストフィールドのプレースホルダーを設定する共通コードが必要です。

html:

<p>Login</p>
<input type="text" data-placeholder="Email" id="txtemail" />
<input type="password" data-placeholder="Password" id="txtpass" />

jQuery:

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

これに対する正しい解決策を書いてください。ありがとうございました

4

3 に答える 3

5

jQuery .data() セレクターを使用できると思います-詳細はこちらをご覧ください- http://api.jquery.com/data/

$(document).ready(function(){
  $('input[type=text]').each(function(){
    var txt = $(this).data('placeholder');
    $(this).attr('placeholder', txt);
  });
});
于 2013-10-31T06:33:26.697 に答える
0
Use this code :

$('input:text').each(function(){
    var placeholder = $(this).data('data-placeholder');
    $(this).attr('placeholder', placeholder);
});
于 2013-10-31T06:38:46.030 に答える
0

こんにちは、この方法を試してください。

<p>Login</p>
<input type="text" data-placeholder="Email" id="txtemail" />
<input type="text" data-placeholder="Password" id="txtpass" data-type="password" />


$.each($('input:text, input:password'), function(key, value){
    $(this).val($(this).attr("data-placeholder"));
    $(this).focus(function(){
        if($(this).attr("data-placeholder") == $(this).val()){
            if($(this).attr('data-type') == "password"){
                $(this).attr('type', 'password')
            }
            $(this).val('');
        }
    });
    $(this).blur(function(){
        if($(this).val() == "" ){
            $(this).val($(this).attr("data-placeholder"));
        }
        if($(this).attr("data-placeholder") == $(this).val()){
            $(this).attr('type', 'text')
    } 
    });
});

jsFiddle のサンプルを参照してくださいhttp://jsfiddle.net/sakNy/2/

于 2013-10-31T07:37:25.940 に答える