2017-01-12 3 views
-1

мне нужна помощь, я имею div как этотИзменение цвета ДИВ с условием на значение

<div id="namacoc" tabindex="1" class="filled-text" placeholder="Input your name" contenteditable ></div> 
<div id="position" tabindex="1" class="filled-text" placeholder="Input your position" contenteditable></div>  
<button type="button" id="btncontinue" class="btn btn-wide btn-blue">Continue</button> 

мой porblem есть, я хочу проверить, что div как проверки. Пример: Если namecoc не имеет value. div namecoc вместо этого изменит цвет на red, когда namecoc имеет значение, которое будет изменено normal color (black), в то время как я выбрал другой div или нажмите кнопку.

ПРИМЕЧАНИЕ: Я использую div, а не форму ввода, потому что bootstrap не изменит мой цвет фона текста на активном. Поэтому я использую div. Извините, мой английский так плохо.

+0

@PankajParkar: и кажется, примечание прочитал мою записку, я наклоняю используемый вход причина самозагрузки оленья кожа позвольте мне тоже создать новый стиль. – Wolfzmus

ответ

1

Я предлагаю вам использовать только input, у контента, редактируемого, есть свои проблемы с разными браузерами.

Попробуйте это.

$("#btncontinue").click(function() { 
 
    $("input").each(function() { 
 
    $.trim($(this).val()) == "" ? 
 
     $(this).addClass("invalid") : $(this).removeClass("invalid"); 
 
    }); 
 
}); 
 

 
$("input").on("blur", function() { 
 
    $.trim($(this).val()) == "" ? 
 
    $(this).addClass("invalid") : $(this).removeClass("invalid"); 
 
});
.invalid { 
 
    border: red 1px solid; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id="namacoc" tabindex="1" class="filled-text" placeholder="Input your name" contenteditable></input> 
 
<input id="position" tabindex="1" class="filled-text" placeholder="Input your position" contenteditable></input> 
 
<button type="button" id="btncontinue" class="btn btn-wide btn-blue">Continue</button>

+0

, но все еще красный, когда я заполнил div и выбрал другой div :( – Wolfzmus

+0

@Wolfzmus вы пытаетесь использовать приведенную выше логику с divs? I вы хотите, чтобы проверка произошла при размытии, тогда мы можем изменить вышеуказанный код. – Deep

+0

oh yeah это ват, я хочу !! Отлично !!! Спасибо! – Wolfzmus

0

Array.prototype.forEach.call(document.querySelectorAll(".filled-text"), function (node) { 
 
    node.onfocus = function() { 
 
    this.className += " active"; 
 

 
    var text = this.innerHTML; 
 

 
    if ((this.id === "namacoc" && text === "Input your name") || 
 
     (this.id === "position" && text === "Input your position")) { 
 

 
     this.innerHTML = ""; 
 
    } 
 
    }; 
 

 
    node.onblur = function() { 
 
    this.className = this.className.replace(" active", ""); 
 

 
    if (this.innerHTML === "") { 
 
     if (this.id === "namacoc") { 
 
     this.innerHTML = "Input your name"; 
 
     } else { 
 
     this.innerHTML = "Input your position"; 
 
     } 
 

 
     this.className = this.className.replace(" normal", "") + " empty"; 
 
    } else { 
 
     this.className = this.className.replace(" empty", "") + " normal"; 
 
    } 
 
    }; 
 
});
.filled-text { 
 
    width: 200px; 
 
    padding: 8px; 
 
    margin: 24px 0; 
 
    border: 1px solid #000; 
 
} 
 

 
.filled-text.placeholder { 
 
    color: #d0d0d0; 
 
} 
 

 
.filled-text.empty { 
 
    color: red; 
 
} 
 

 
.filled-text.active, 
 
.filled-text.normal { 
 
    color: #000; 
 
}
<div id="namacoc" class="filled-text placeholder" contenteditable>Input your name</div> 
 
<div id="position" class="filled-text placeholder" contenteditable>Input your position</div>

Смежные вопросы