[英]jQuery toggle focus / blur
How can I toggle an element's CSS on focus/blur with jQuery? 如何使用jQuery在焦点/模糊上切换元素的CSS?
$('.answerSpace').bind('blur', function(){
$('.normProf').toggleClass('opacProf');
});
$('.answerSpace').bind('focus', function(){
$('.opacProf').toggleClass('normProf');
});
So now I have this. 所以现在我有了这个。 But it doesn't quite work...
但它不太有用......
Check this out, it's exactly how y'all should do jQuery focus toggle.. 看看这个,这正是你们应该怎么做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 } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input>
Try 尝试
$('.answerSpace').bind('blur', function(){ $('.normProf').removeClass("normProf").addClass('opacProf'); });
$('.answerSpace').bind('focus', function(){ $('.opacProf').removeClass("opacProf").addClass('normProf'); });
Well, if I get you right, you can use onblur and onfocus events, with the toggleClass function: 好吧,如果我找到了你,你可以使用toggleClass函数使用onblur和onfocus事件:
$('#yourelement').bind('blur', function(){
$(this).toggleClass('your-class');
});
$('#yourelement').bind('focus', function(){
$(this).toggleClass('your-class');
});
Blur is only get active when you leave a input, so you can use it to remove the focus again. 只有在您输入时才会激活模糊,因此您可以使用它来再次移除焦点。
$('input').focus(function(){
$(this).css('box-shadow', '0px 0px 1px #ccc');
});
$('input').blur(function(){
$(this).css('box-shadow', 'none');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.