jQuery切换焦点/模糊

hub*_*rid 8 jquery focus toggle

如何使用jQuery在焦点/模糊上切换元素的CSS?

$('.answerSpace').bind('blur', function(){
$('.normProf').toggleClass('opacProf');
});

$('.answerSpace').bind('focus', function(){
    $('.opacProf').toggleClass('normProf');
});
Run Code Online (Sandbox Code Playgroud)

所以现在我有了这个.但它不太有用......

vsy*_*ync 18

看看这个,这正是你们应该怎么做jQuery焦点切换..

基本用例:

$('input').on('focus blur', toggleFocus);

function toggleFocus(e){
    console.log(e.type)

    if( e.type == 'focusin' ){ 
      // do something on focus
    }
    else{
      // do something else on blur
    }
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input>
Run Code Online (Sandbox Code Playgroud)


mat*_*ven 11

尝试

$('.answerSpace').bind('blur', function(){ $('.normProf').removeClass("normProf").addClass('opacProf'); });
$('.answerSpace').bind('focus', function(){ $('.opacProf').removeClass("opacProf").addClass('normProf'); });
Run Code Online (Sandbox Code Playgroud)


blu*_*oot 7

好吧,如果我找到了你,你可以使用toggleClass函数使用onbluronfocus事件:

$('#yourelement').bind('blur', function(){
    $(this).toggleClass('your-class');
});

$('#yourelement').bind('focus', function(){
    $(this).toggleClass('your-class');
});
Run Code Online (Sandbox Code Playgroud)

  • 你可以结合`bind('模糊焦点')` (2认同)