1

このコードは完全に機能しており、画像で事前入力フィールドをシミュレートしています。

<script>
$('#example).focus(function(){
    var newValue = $(this).val();
    if ($(this).val() == ""){
        $(this).css("background-image", "none");
    }else{
        $(this).val(newValue);
    }
}).blur(function(){
    var newValue = $(this).val();
    if ($(this).val() == ""){
             $(this).css("background-image", "url(assets/templates/herveou-coiffure/css/pre_input.png)");
}else{
        $(this).val(newValue);
    }
});
</script>

いくつかの入力フィールド(少なくとも2つ)でこれを使用する必要があるので、次のようなことを試しました:

<script>
$('#example, example2').focus(function(){
    var newValue = $(this).val();
    if ($(this).val() == ""){
        $(this).css("background-image", "none");
    }else{
        $(this).val(newValue);
    }
}).blur(function(){
    var newValue = $(this).val();
    if ($(this).val() == ""){
       if ($('#example2')[0]){
   // Do something here if an element with this class exists
 $(this).css("background-image", "url(assets/templates/herveou-coiffure/css/headers.jpg)");}
       if ($('#example')[0]){
   // Do something here if an element with this class exists
 $(this).css("background-image", "url(assets/templates/herveou-coiffure/css/pre_input.png)");}
}else{
        $(this).val(newValue);
    }
});
</script>

もちろん、私はjavascritpの専門家ではなく、うまくいきません。誰でも助けてくれますか?? どうもありがとうございました...

4

2 に答える 2

0

あなたが書くとき $('<selector1>, <selector2> , ....'。各パラメーターに適切なセレクターを記述する必要があります。だからあなたは変わるべき$('#example, example2')です$('#example, #example2').

于 2013-04-30T12:22:44.147 に答える