开发者

jQuery, writing if statement with .bind vs .focus looking for alternate way to execute

开发者 https://www.devze.com 2023-01-04 16:43 出处:网络
Brief synopsis, this code works beautifully and does what it is supposed except with radio buttons on webkit.

Brief synopsis, this code works beautifully and does what it is supposed except with radio buttons on webkit.

$('input, textarea, select').focus(function(){
    $(this).parents('.row').addClass("hilite");
        }).blur(function(){
            $(this).parents('.row2').removeClass("hilite");
});

I did some research and tested it a few times and using

$('input, textarea, select').bind('change'(){
    $(this).parents('.row').addClass("hilite");
        }).blur(function(){
            $(this).parents('.row').removeClass("hilite");
});

I am able to get it to work with webkit radio buttons but it does not acknowledge the blur and therefor does not remove the hilite. So is there an alternate way to write this so it will remove the class on blur vs mouseleave (which does work). As alwa开发者_如何学Goys THANKS IN ADVANCE!


Try giving the :radio buttons a click event to force focus. Seems to work in webkit.

Try it out: http://jsfiddle.net/5dDQn/

$(':radio').click(function() {
    $(this).focus();
});


This approach probably doesn't qualify as 'pretty' but I'd try like so:

$('input, textarea, select')
  .focus(function() {
    $(this).parents('.row').addClass('hilite');
    if ($(this).is(':radio')) {
      $(this).one('mouseleave', function () {
        $(this).parents('.row').removeClass('hilite');
      });
    }
  })
  .blur(function() {
    $(this).parents('.row2').removeClass('hilite');
  });

That relies on what I believe you indicated - the radio buttons will accept focus, don't seem to trigger blur, but mouseleave seems to work properly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号