sam*_*yb8 13 javascript text background input colors
如果输入为空,我正在用这个javascript代码来改变文本输入的背景颜色.
这是代码:
function checkFilled() {
var inputVal = document.getElementById("subEmail").value;
if (inputVal == "") {
inputVal.style.backgroundColor = "yellow";
}
}
Run Code Online (Sandbox Code Playgroud)
我希望文本框一开始就是黄色的.
Dha*_*hak 27
演示 --> http://jsfiddle.net/2Xgfr/829/
HTML
<input type="text" id="subEmail" onchange="checkFilled();">
Run Code Online (Sandbox Code Playgroud)
JavaScript的
function checkFilled() {
var inputVal = document.getElementById("subEmail");
if (inputVal.value == "") {
inputVal.style.backgroundColor = "yellow";
}
else{
inputVal.style.backgroundColor = "";
}
}
checkFilled();
Run Code Online (Sandbox Code Playgroud)
注意:您正在检查值并将颜色设置为不允许的值,这就是它给您错误的原因.试试如上.