javascript - 如何删除此代码中的重复?
问题描述
我只是在调用一个函数highlightInput(this)
,它只是改变所选输入的颜色。我认为可能有更好的方法来避免重复。有任何想法吗?
HTML 文件
<div class="form-group">
<label for="your_name">Your name:</label>
<input type="text" class="form-control" name="your_name" onfocus="highlightInput(this);">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="email"onfocus="highlightInput(this);">
</div>
<div class="form-group">
<label for="title">Event title:</label>
<input type="text" class="form-control" name="title"onfocus="highlightInput(this);">
</div>
<div class="form-group">
<label for="location">Event location:</label>
<input type="text" class="form-control" name="location"onfocus="highlightInput(this);">
</div>
highlightInput 的样子:
var Color = {
inputColor:function (color, self){
$(self).css('backgroundColor', color);
}
}
function highlightInput(self){
Color.inputColor('lightyellow', self);
$(self).blur(function(){
Color.inputColor('white', self);
});
}
解决方案
摆脱 jQuery 并使用 CSS 来完成。
input.form-control:focus {
background-color: lightyellow;
}
<input type="text" class="form-control">
推荐阅读
- javascript - 选择其他运输方式时隐藏元素
- hadoop - 如果在 Java MapReduce 中使用 Bulk Load API,如何恢复数据?
- python-3.x - 基本概念:如何在python中定义一个链表?
- apache-spark - avro 分区修剪如何在内部工作?
- discord - DiscordJS 角色集返回 API 错误:缺少权限
- python-3.x - 如何在 django 管理页面中使用 ImportExportModelAdmin
- flutter - Flutter Pusher Websocket 包不工作
- css - 在 Reddit 上将 spritesheet 位置设置为锚标记的背景
- docker - Docker 正在监听运行命令中指定的端口
- java - 使用 Tomcat 9 时无法让 log4j 写入日志文件