jQuery focusin / focusout。避免专注于提交

时间:2013-11-14 05:47:58

标签: jquery submit focusout focusin

当我专注于表格时,我已经有了这个小脚本来切换快乐的脸:

/*  Happy face script */
$('#search').on('focusin', function() {
   $("#smile").removeClass("fa-meh-o");
   $("#smile").addClass("fa-smile-o");
});
$('#search').on('focusout', function() {
   $("#smile").removeClass("fa-smile-o");
   $("#smile").addClass("fa-meh-o");
});

然而,当我click 'submit'时,它会回到focusout状态。我该如何避免呢?

1 个答案:

答案 0 :(得分:0)

删除focusoutevent并使用toggleClass()之类的,

$('#search').on('focusin', function() {
   $("#smile").toggleClass("fa-meh-o fa-smile-o");
});


$('#submit')on('click',function(){
   // your code then you can remove the classes again if not then remove below code
   $("#smile").toggleClass("fa-meh-o fa-smile-o");
});

已更新您可以focusin and focusout使用相同的toggleClass

$('#search').on('focusin focusout', function() {
   $("#smile").toggleClass("fa-meh-o fa-smile-o");
});