2010-06-26 20 views
1

簡要概要,此代碼運行得非常漂亮,除了使用webkit上的單選按鈕之外,它的功能還不錯。jQuery,使用.bind編寫if語句與.focus尋找替代方法來執行

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

我做了一些研究和測試了幾次,並使用

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

我能得到它與WebKit的單選按鈕的工作,但它不承認的模糊和爲此不刪除hilite。那麼有沒有其他的方法來寫這個,所以它會刪除模糊vs mouseleave上的類(它可以工作)。一如既往的感謝!

回答

1

嘗試給:radio按鈕一個點擊事件強制焦點。似乎在webkit中工作。

試試看:http://jsfiddle.net/5dDQn/

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

這個伎倆!謝謝! – CarterMan 2010-06-26 20:58:35

0

這種做法可能沒有資格作爲「漂亮」,但我想嘗試像這樣:

$('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'); 
    }); 

依靠什麼,我相信你指出 - 單選按鈕將接受焦點,似乎不會觸發模糊,但mouseleave似乎正常工作。

+0

收音機看起來不像收音機那樣得到「焦點」事件。這是我發佈的jsFiddle,但沒有添加「click」事件。 http://jsfiddle.net/5dDn1/你會看到'focus'不會觸發webkit中的收音機。我同意,這個問題聽起來像是火了。 – user113716 2010-06-26 16:23:09

+0

我很抱歉,我應該更多地表述它。 – CarterMan 2010-06-26 21:26:17