焦点函数在Jquery中

时间:2015-03-22 16:30:52

标签: jquery function focus

此功能在单击时将文本框的背景颜色更改为红色。我想知道在没有点击的时候如何让它恢复原来的颜色?

$('#textfield').focus(function() {
    $(this).css('background-color','red');
}); 

1 个答案:

答案 0 :(得分:0)

您可以使用bluraddClass()removeClass()

$('#textfield').focus(function() {
    $(this).addClass('red');
}); 
$('#textfield').blur(function() {
    $(this).removeClass('red');
}); 
.red{
  background-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input tpye="text" id="textfield" value=""/>