【发布时间】:2021-03-07 22:29:06
【问题描述】:
我的问题是,当我点击输入后留空时,我想让输入的背景变为红色。
这是我的 HTML:
<div id="name">
<label>Name<sup></sup></label> <br>
<input id="first" style="font-size:11pt;" type="text" name="name" placeholder="First Name" onChange='emptyField("first")'/>
<input id="last" style="font-size:11pt;" type="text" name="name" placeholder="Last Name" onChange='emptyField("last")'/>
</div>
这是我的 javascript 程序:
function emptyField(x) {
var field = document.getElementById(x);
if(field !== ""){
field.style.backgroundColor = "white";
}
else {
field.style.backgroundColor = "red";
}}
顺便提一下,我刚刚开始学习 HTML、CSS、JS 和 PHP。所以希望这对我来说不会太复杂。
【问题讨论】:
标签: javascript html input background-color