2012-08-08 70 views
0

按鈕上的懸停事件如下。使用jquery懸停在按鈕上?

<div id="common_id"> 
<div class="button" id="make_larger"> 
<button type="button">Large</button> 
</div> 
<div class="button" id="make_small"> 
<button type="button">Smaller</button> 
</div> 
<div class="button" id="make_normal"> 
<button type="button">Normal</button> 
</div> 
</div> 

JQuery的代碼是:

$('#common_id .button').hover(function() { 
      $(this).addClass('hover1'); 
     }, function() { 
      $(this).removeClass('hover1'); 

     }); 

CSS是:

.hover1 { 

background-color:Black; 
position:relative; 

}  

當放置鼠標按鈕時,完整的行(從開始頁的端部)是越來越黑甚至按鈕沒有變黑,但它的背景似乎在那一排上是黑色的。 我想讓按鈕在我懸停時變黑。 爲什麼它不起作用?

在此先感謝。

+1

那豈不是更容易使用的CSS' :懸停'假選擇器?使用CSS規則'#common_id .button:hover {}'。 – 2012-08-08 10:58:50

回答

0

在div走的是100%的寬度。所以不要選擇div來選擇按鈕。所以,你的選擇應該是這樣的

$('#common_id .button button').hover(function() { 
      $(this).addClass('hover1'); 
     }, function() { 
      $(this).removeClass('hover1'); 

     });​ 

試試這個fiddle

+0

太好了。 謝謝先生。 .button是一個div類。請告訴第二個「按鈕」標籤是什麼意思? – user1544975 2012-08-08 11:01:57

+0

選擇器裝置中的第二個按鈕。 。 。選擇所有按鈕標籤。因此,選擇器語句意味着jQuery正在尋找包含在具有id爲'common_id'的元素中的'button'類的元素中包含的按鈕標記。 – DaveR 2012-08-08 11:12:44

+0

第二個按鈕只是在那裏使用的元素「」 – Paras 2012-08-08 11:13:05

2

完全卸下jQuery和使用:

#common_id .button:hover { 
    background-color:Black; 
    position:relative; 
} 
0

我認爲CSS唯一的答案是最好的 - 但是你可以通過使用jQuery選擇$(「按鈕」)選擇頁面上的按鈕,就像這個例子:http://jsfiddle.net/2fH7f/

1

Sohhee的答案是正確的,但我認爲你需要的CSS腳本應該

#common_id .button botton:hover { 
    background-color:Black; 
    position:relative; 
}